mirror of
https://gitea.com/mcereda/oam.git
synced 2026-02-08 21:34:25 +00:00
feat(pulumi/examples): add generic aws ec2 instance
This commit is contained in:
2
examples/pulumi/aws/ec2 instance/.env.fish
Normal file
2
examples/pulumi/aws/ec2 instance/.env.fish
Normal file
@@ -0,0 +1,2 @@
|
||||
set -x PULUMI_BACKEND_URL 'file://.'
|
||||
set -x PULUMI_CONFIG_PASSPHRASE 'test123'
|
||||
3
examples/pulumi/aws/ec2 instance/.gitignore
vendored
Normal file
3
examples/pulumi/aws/ec2 instance/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/bin/
|
||||
/node_modules/
|
||||
/package-lock.json
|
||||
8
examples/pulumi/aws/ec2 instance/Pulumi.any.yaml
Normal file
8
examples/pulumi/aws/ec2 instance/Pulumi.any.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
encryptionsalt: v1:55yDA5Kuyzs=:v1:+kFXkziA9Bd7nNZQ:OSBtNRAVCGBXwzTtHOGA5Ti9Dz+FTQ==
|
||||
config:
|
||||
aws:region: eu-west-1
|
||||
aws:defaultTags:
|
||||
tags:
|
||||
ManagedByPulumi: true
|
||||
Owner: "somebody@example.com"
|
||||
PulumiProject: ec2-instance
|
||||
9
examples/pulumi/aws/ec2 instance/Pulumi.yaml
Normal file
9
examples/pulumi/aws/ec2 instance/Pulumi.yaml
Normal file
@@ -0,0 +1,9 @@
|
||||
name: ec2-instance
|
||||
runtime: nodejs
|
||||
description: AWS EC2 instance example
|
||||
config:
|
||||
pulumi:tags:
|
||||
value:
|
||||
pulumi:template: aws-typescript
|
||||
backend:
|
||||
url: file://.
|
||||
122
examples/pulumi/aws/ec2 instance/index.ts
Normal file
122
examples/pulumi/aws/ec2 instance/index.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import * as aws from "@pulumi/aws";
|
||||
import * as cloudinit from "@pulumi/cloudinit";
|
||||
import * as yaml from "yaml";
|
||||
|
||||
const ami = aws.ec2.getAmiOutput({
|
||||
owners: [ "amazon", ],
|
||||
nameRegex: "^al2023-ami-2023.*",
|
||||
filters: [
|
||||
{
|
||||
name: "architecture",
|
||||
values: [ "arm64" ],
|
||||
},
|
||||
{
|
||||
name: "state",
|
||||
values: [ "available" ],
|
||||
},
|
||||
],
|
||||
mostRecent: true,
|
||||
});
|
||||
const keyPair = aws.ec2.getKeyPairOutput({ keyName: "somebody-ec2Instances" });
|
||||
const subnet = aws.ec2.getSubnetOutput({
|
||||
filters: [{
|
||||
name: "tag:Name",
|
||||
values: [ "Private C" ],
|
||||
}],
|
||||
});
|
||||
|
||||
const securityGroup = new aws.ec2.SecurityGroup(
|
||||
"ec2-instance-example",
|
||||
{
|
||||
name: "Ec2InstanceExample",
|
||||
description: "Regulate communications to and from the EC2 Instance",
|
||||
tags: {
|
||||
Name: "EC2 Instance Example",
|
||||
},
|
||||
},
|
||||
);
|
||||
const role = new aws.iam.Role(
|
||||
"ec2-instance-example",
|
||||
{
|
||||
name: "Ec2InstanceExample",
|
||||
assumeRolePolicy: JSON.stringify({
|
||||
Version: "2012-10-17",
|
||||
Statement: [{
|
||||
Effect: "Allow",
|
||||
Action: "sts:AssumeRole",
|
||||
Principal: {
|
||||
Service: "ec2.amazonaws.com",
|
||||
},
|
||||
}],
|
||||
}),
|
||||
managedPolicyArns: [ "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" ],
|
||||
},
|
||||
);
|
||||
const instanceProfile = new aws.iam.InstanceProfile(
|
||||
"ec2-instance-example",
|
||||
{
|
||||
name: "Ec2InstanceExample",
|
||||
role: role.name,
|
||||
},
|
||||
);
|
||||
const userData = new cloudinit.Config(
|
||||
"ec2-instance-example",
|
||||
{
|
||||
gzip: true,
|
||||
base64Encode: true,
|
||||
parts: [
|
||||
{
|
||||
contentType: "text/cloud-config",
|
||||
content: yaml.stringify({
|
||||
package_upgrade: false,
|
||||
packages: [ "amazon-ssm-agent" ],
|
||||
runcmd: [
|
||||
"systemctl daemon-reload",
|
||||
"systemctl enable --now 'amazon-ssm-agent.service'",
|
||||
]
|
||||
}),
|
||||
filename: "cloud-config.managed-by.ssm.yml",
|
||||
},
|
||||
{
|
||||
contentType: "text/cloud-config",
|
||||
content: yaml.stringify({
|
||||
package_upgrade: false,
|
||||
packages: [ "python" ],
|
||||
}),
|
||||
filename: "cloud-config.managed-by.ansible.yml",
|
||||
mergeType: "dict(recurse_array,no_replace)+list(append)",
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
new aws.ec2.Instance(
|
||||
"ec2-instance-example",
|
||||
{
|
||||
ami: ami.apply(ami => ami.id),
|
||||
iamInstanceProfile: instanceProfile.name,
|
||||
instanceType: "t4g.small",
|
||||
keyName: keyPair.apply(keyPair => keyPair.keyName!),
|
||||
rootBlockDevice: {
|
||||
volumeType: "gp3",
|
||||
volumeSize: 10,
|
||||
tags: {
|
||||
Description: "Instance root disk",
|
||||
Name: "EC2 Instance Example",
|
||||
},
|
||||
},
|
||||
subnetId: subnet.apply(subnet => subnet.id),
|
||||
tags: {
|
||||
Name: "EC2 Instance Example",
|
||||
ManagedBySsm: "true",
|
||||
ManagedByAnsible: "true",
|
||||
},
|
||||
userData: userData.rendered,
|
||||
vpcSecurityGroupIds: [ securityGroup.id ],
|
||||
},
|
||||
{
|
||||
ignoreChanges: [
|
||||
// avoid being replaced just because a new version of the base image came out
|
||||
"ami",
|
||||
],
|
||||
}
|
||||
);
|
||||
13
examples/pulumi/aws/ec2 instance/package.json
Normal file
13
examples/pulumi/aws/ec2 instance/package.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "ec2-instance",
|
||||
"main": "index.ts",
|
||||
"devDependencies": {
|
||||
"@types/node": "^18",
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pulumi/aws": "^6.0.0",
|
||||
"@pulumi/cloudinit": "^1.0.0",
|
||||
"yaml": "^2.0.0"
|
||||
}
|
||||
}
|
||||
18
examples/pulumi/aws/ec2 instance/tsconfig.json
Normal file
18
examples/pulumi/aws/ec2 instance/tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"outDir": "bin",
|
||||
"target": "es2020",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"experimentalDecorators": true,
|
||||
"pretty": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noImplicitReturns": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user