Using Aws Ecs With Cloudfront
Introduction
This Pulumi program sets up an AWS ECS cluster with a CloudFront distribution. The ECS cluster will run a sample application, and the CloudFront distribution will serve the application to users globally.
Step-by-Step Explanation
Step 1: Set up the VPC and Subnets
First, we need to create a VPC and subnets for our ECS cluster.
Step 2: Create an ECS Cluster
Next, we’ll create an ECS cluster to run our application.
Step 3: Define the Task Definition
We’ll define a task definition for our ECS service, specifying the container image and other settings.
Step 4: Create an ECS Service
We’ll create an ECS service to run the task definition in our cluster.
Step 5: Set up an Application Load Balancer
We’ll set up an Application Load Balancer (ALB) to distribute traffic to our ECS service.
Step 6: Create a CloudFront Distribution
Finally, we’ll create a CloudFront distribution to serve the application globally.
Conclusion
In this Pulumi program, we set up an AWS ECS cluster with a CloudFront distribution. This setup allows us to run a containerized application in ECS and serve it globally using CloudFront.
Full Code Example
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Step 1: Set up the VPC and Subnets
const vpc = new aws.ec2.Vpc("my-vpc", {
cidrBlock: "10.0.0.0/16",
});
const subnet1 = new aws.ec2.Subnet("my-subnet-1", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
});
const subnet2 = new aws.ec2.Subnet("my-subnet-2", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
availabilityZone: "us-west-2b",
});
// Step 2: Create an ECS Cluster
const cluster = new aws.ecs.Cluster("my-cluster");
// Step 3: Define the Task Definition
const taskDefinition = new aws.ecs.TaskDefinition("my-task", {
family: "my-task-family",
containerDefinitions: JSON.stringify([{
name: "my-app",
image: "nginx",
essential: true,
portMappings: [{
containerPort: 80,
hostPort: 80,
}],
}]),
});
// Step 4: Create an ECS Service
const service = new aws.ecs.Service("my-service", {
cluster: cluster.arn,
taskDefinition: taskDefinition.arn,
desiredCount: 2,
launchType: "EC2",
networkConfiguration: {
subnets: [subnet1.id, subnet2.id],
securityGroups: [],
},
});
// Step 5: Set up an Application Load Balancer
const alb = new aws.lb.LoadBalancer("my-alb", {
internal: false,
loadBalancerType: "application",
securityGroups: [],
subnets: [subnet1.id, subnet2.id],
});
const targetGroup = new aws.lb.TargetGroup("my-target-group", {
port: 80,
protocol: "HTTP",
targetType: "instance",
vpcId: vpc.id,
});
const listener = new aws.lb.Listener("my-listener", {
loadBalancerArn: alb.arn,
port: 80,
defaultActions: [{
type: "forward",
targetGroupArn: targetGroup.arn,
}],
});
// Update ECS Service with Target Group ARN
const updatedService = new aws.ecs.Service("updated-service", {
cluster: cluster.arn,
taskDefinition: taskDefinition.arn,
desiredCount: 2,
launchType: "EC2",
networkConfiguration: {
subnets: [subnet1.id, subnet2.id],
securityGroups: [],
},
loadBalancers: [{
targetGroupArn: targetGroup.arn,
containerName: "my-app",
containerPort: 80,
}],
});
// Step 6: Create a CloudFront Distribution
const cloudFrontDistribution = new aws.cloudfront.Distribution("my-distribution", {
origins: [{
domainName: alb.dnsName,
originId: alb.arn,
}],
enabled: true,
defaultCacheBehavior: {
targetOriginId: alb.arn,
viewerProtocolPolicy: "redirect-to-https",
allowedMethods: ["GET", "HEAD"],
cachedMethods: ["GET", "HEAD"],
forwardedValues: {
queryString: false,
cookies: { forward: "none" },
},
},
priceClass: "PriceClass_100",
restrictions: {
geoRestriction: {
restrictionType: "none",
},
},
viewerCertificate: {
cloudfrontDefaultCertificate: true,
},
});
// Export outputs
export const vpcId = vpc.id;
export const subnetIds = [subnet1.id, subnet2.id];
export const ecsClusterName = cluster.name;
export const ecsServiceName = updatedService.name;
export const loadBalancerDnsName = alb.dnsName;
export const cloudFrontUrl = cloudFrontDistribution.domainName;
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.