Skip to content

Instantly share code, notes, and snippets.

@fadur
Last active April 5, 2019 12:41
Show Gist options
  • Save fadur/2a08006e2fdacf620e9cc199b4899d0a to your computer and use it in GitHub Desktop.
Save fadur/2a08006e2fdacf620e9cc199b4899d0a to your computer and use it in GitHub Desktop.
import cdk = require('@aws-cdk/cdk');
import ecs = require('@aws-cdk/aws-ecs');
import ec2 = require('@aws-cdk/aws-ec2');
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import ecr = require('@aws-cdk/aws-ecr')
export class AppgatewayStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.VpcNetwork(this, 'testVPC', {
maxAZs: 3 // Default is all AZs in region
});
const cluster = new ecs.Cluster(this, 'testCluster', {
vpc: vpc
});
const task = new ecs.FargateTaskDefinition(this, 'testTask',{
memoryMiB: '512', // Default is 512
cpu: '256', // Default is 256
})
const repo = new ecr.Repository(this, 'testApp', {
repositoryName: 'testAppRepo'
})
const container = task.addContainer('testAppContainer', {
image: ecs.ContainerImage.fromEcrRepository(repo, 'latest'), // Required
})
container.addPortMappings({
containerPort: 8080
})
// Create a load-balanced Fargate service and make it public
const service = new ecs.FargateService(this, 'testAppService', {
cluster,
taskDefinition: task,
desiredCount: 2,
})
const lb = new elbv2.ApplicationLoadBalancer(this, 'AppLB', {
vpc,
internetFacing: true
});
const listener = lb.addListener('AppLBListener', {port: 80})
listener.addTargets('ECS', {port: 80, targets: [service]})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment