Created
May 26, 2024 14:02
-
-
Save BharatKalluri/64035189197c1e394e5707d09886da94 to your computer and use it in GitHub Desktop.
Hello world stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import "source-map-support/register"; | |
import * as cdk from "aws-cdk-lib"; | |
import {Construct} from "constructs"; | |
import {IpProtocol, SubnetType} from "aws-cdk-lib/aws-ec2"; | |
const envDetails = {account: "<accountId>", region: "ap-south-1"}; | |
const vpcName = 'uat-vpc' | |
export class NetworkStack extends cdk.Stack { | |
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
new cdk.aws_ec2.Vpc(this, "vpc", { | |
vpcName: vpcName, | |
availabilityZones: ['ap-south-1a', 'ap-south-1b', 'ap-south-1c'], | |
ipAddresses: cdk.aws_ec2.IpAddresses.cidr('10.0.0.0/16'), | |
createInternetGateway: true, | |
enableDnsHostnames: true, | |
enableDnsSupport: true, | |
natGateways: 1, | |
ipProtocol: IpProtocol.IPV4_ONLY, | |
subnetConfiguration: [{ | |
subnetType: SubnetType.PUBLIC, | |
name: 'public', | |
mapPublicIpOnLaunch: false, | |
cidrMask: 24, | |
}, { | |
subnetType: SubnetType.PRIVATE_WITH_EGRESS, | |
name: 'private', | |
cidrMask: 24, | |
}], | |
}); | |
} | |
} | |
export class HelloWorldApplicationStack extends cdk.Stack { | |
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
const applicationId = 'HelloWorldApplication' | |
const vpc = cdk.aws_ec2.Vpc.fromLookup(this, 'vpc', {vpcName: vpcName}) | |
const applicationLoadBalancer = new cdk.aws_elasticloadbalancingv2.ApplicationLoadBalancer(this, `${applicationId}LoadBalancer`, { | |
vpc: vpc, | |
internetFacing: true, | |
vpcSubnets: {subnetType: SubnetType.PUBLIC} | |
}); | |
const taskRole = new cdk.aws_iam.Role(this, `${applicationId}TaskRole`, { | |
assumedBy: new cdk.aws_iam.ServicePrincipal('ecs-tasks.amazonaws.com'), | |
managedPolicies: [{managedPolicyArn: 'arn:aws:iam::aws:policy/SecretsManagerReadWrite'}, {managedPolicyArn: 'arn:aws:iam::aws:policy/AmazonS3FullAccess'}] | |
}) | |
const helloWorldApplication = new cdk.aws_ecs_patterns.ApplicationLoadBalancedFargateService(this, applicationId, { | |
vpc: vpc, | |
taskSubnets: {subnetType: SubnetType.PRIVATE_WITH_EGRESS}, | |
loadBalancer: applicationLoadBalancer, | |
assignPublicIp: false, | |
cpu: 2048, | |
memoryLimitMiB: 4096, | |
taskImageOptions: { | |
image: new cdk.aws_ecs.RepositoryImage('crccheck/hello-world:latest'), | |
containerPort: 8000, | |
environment: { | |
'NODE_ENV': 'uat' | |
}, | |
taskRole: taskRole, | |
}, | |
}); | |
const scalableTarget = new cdk.aws_applicationautoscaling.ScalableTarget(this, `${applicationId}ScalingTarget`, { | |
serviceNamespace: cdk.aws_applicationautoscaling.ServiceNamespace.ECS, | |
resourceId: `service/${helloWorldApplication.cluster.clusterName}/${helloWorldApplication.service.serviceName}`, | |
scalableDimension: 'ecs:service:DesiredCount', | |
minCapacity: 1, | |
maxCapacity: 3, | |
}); | |
const scalingPolcy = new cdk.aws_applicationautoscaling.TargetTrackingScalingPolicy(this, `${applicationId}ScalingPolicy`, { | |
scalingTarget: scalableTarget, | |
targetValue: 70, | |
scaleOutCooldown: cdk.Duration.seconds(60), | |
scaleInCooldown: cdk.Duration.seconds(60), | |
predefinedMetric: cdk.aws_applicationautoscaling.PredefinedMetric.ECS_SERVICE_AVERAGE_CPU_UTILIZATION | |
}); | |
} | |
} | |
const app = new cdk.App(); | |
new NetworkStack(app, "NetworkStack", {env: envDetails}); | |
new HelloWorldApplicationStack(app, "HelloWorldApplicationStack", {env: envDetails}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment