Skip to content

Instantly share code, notes, and snippets.

@cnunciato
Created June 2, 2023 01:12
Show Gist options
  • Save cnunciato/812531c537ccf9a0e94674d11cd62ef7 to your computer and use it in GitHub Desktop.
Save cnunciato/812531c537ccf9a0e94674d11cd62ef7 to your computer and use it in GitHub Desktop.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
const config = new pulumi.Config();
const containerPort = config.getNumber("containerPort") || 80;
const cpu = config.getNumber("cpu") || 512;
const memory = config.getNumber("memory") || 128;
const vpc = new awsx.ec2.Vpc("vpc", {
enableDnsHostnames: true,
});
export const { publicSubnetIds } = vpc;
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
vpcId: vpc.vpcId,
egress: [
{
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
protocol: "-1",
},
],
ingress: [
{
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
protocol: "-1",
},
],
});
const cluster = new aws.ecs.Cluster("cluster");
// An ALB to serve the container endpoint to the internet
const loadbalancer = new awsx.lb.ApplicationLoadBalancer("loadbalancer", {
subnetIds: vpc.publicSubnetIds,
securityGroups: [
securityGroup.id,
],
});
// An ECR repository to store our application's container image
const repo = new awsx.ecr.Repository("repo", {
forceDelete: true,
});
// Build and publish our application's container image from ./app to the ECR repository
const image = new awsx.ecr.Image("image", {
repositoryUrl: repo.url,
path: "./app",
env: {
DOCKER_DEFAULT_PLATFORM: "linux/amd64",
}
});
const service = new awsx.ecs.FargateService("service", {
cluster: cluster.arn,
networkConfiguration: {
subnets: vpc.privateSubnetIds,
securityGroups: [
securityGroup.id
],
},
desiredCount: 2,
taskDefinitionArgs: {
container: {
image: image.imageUri,
cpu: cpu,
memory: memory,
essential: true,
portMappings: [{
containerPort: containerPort,
targetGroup: loadbalancer.defaultTargetGroup,
}],
},
},
});
// The URL at which the container's HTTP endpoint will be available
export const url = pulumi.interpolate`http://${loadbalancer.loadBalancer.dnsName}`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment