Use Pulumi Crosswalk to setup an ECS Service in a VPC
How to use Pulumi infrastructure as code SDK to deploy a load balanced ECS service in a VPC
Pulumi is an infrastructure as code framework for software engineers. Instead of writing YAML to define your infrastructure you can use higher level SDK commands, in a familiar programming language, and Pulumi will create the necessary resources for you automatically.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// File: service-in-vpc.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
const vpc = new awsx.ec2.Vpc("vpc", {});
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
vpcId: vpc.vpcId,
egress: [{
fromPort: 0,
toPort: 0,
protocol: "-1",
cidrBlocks: ["0.0.0.0/0"],
ipv6CidrBlocks: ["::/0"],
}],
});
const cluster = new aws.ecs.Cluster("cluster", {});
const service = new awsx.ecs.FargateService("service", {
cluster: cluster.arn,
networkConfiguration: {
subnets: vpc.privateSubnetIds,
securityGroups: [securityGroup.id],
},
desiredCount: 2,
taskDefinitionArgs: {
container: {
image: "nginx:latest",
cpu: 512,
memory: 128,
essential: true,
},
},
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// File: service-in-vpc.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
const vpc = new awsx.ec2.Vpc("vpc", {});
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
vpcId: vpc.vpcId,
egress: [{
fromPort: 0,
toPort: 0,
protocol: "-1",
cidrBlocks: ["0.0.0.0/0"],
ipv6CidrBlocks: ["::/0"],
}],
});
const cluster = new aws.ecs.Cluster("cluster", {});
const service = new awsx.ecs.FargateService("service", {
cluster: cluster.arn,
networkConfiguration: {
subnets: vpc.privateSubnetIds,
securityGroups: [securityGroup.id],
},
desiredCount: 2,
taskDefinitionArgs: {
container: {
image: "nginx:latest",
cpu: 512,
memory: 128,
essential: true,
},
},
});Setup
- Ensure that you have Pulumi setup on your system and configured to connect to AWS .
- Start a new Pulumi project
- Copy the code above into your Pulumi project
Usage
Show a preview of resources to be deployed:
1
pulumi previewDeploy the resources to your AWS account:
1
pulumi upPrint out the URL of the deployed ECS service's load balancer:
1
pulumi stack output urlTear down the stack and all of its resources:
1
pulumi destroyEnjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article