in cdk/lib/FargateServiceStack.ts [18:80]
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const asset = new DockerImageAsset(this, 'ddb-local-image', {
directory: path.join(__dirname, "..", ".."),
});
const vpc = new ec2.Vpc(this, "EcsVpc", {
maxAzs: 3 // Default is all AZs in region
});
const cluster = new ecs.Cluster(this, "TestCluster", {
vpc: vpc,
clusterName: "ddb-local-cluster",
containerInsights: false
});
const logGroup = new LogGroup(this, "FargateLogGroup", {
logGroupName: "/ecs/ddb-local-service"
})
const taskDef = new ecs.FargateTaskDefinition(this, "MyTask", {
cpu: 512,
memoryLimitMiB: 1024,
})
const container = new ecs.ContainerDefinition(this, "MyContainer", {
image: ContainerImage.fromDockerImageAsset(asset),
taskDefinition: taskDef,
environment: {
PARAM1: "test1"
},
logging: new ecs.AwsLogDriver({
logGroup: logGroup,
streamPrefix: `ddb-local-service`,
})
}
)
const service = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", {
cluster: cluster, // Required
cpu: 512, // Default is 256
desiredCount: 2, // Default is 1
taskImageOptions: { image: ecs.RepositoryImage.fromDockerImageAsset(asset) },
memoryLimitMiB: 2048, // Default is 512
publicLoadBalancer: true // Default is false
});
const DDB_TABLE_ARN = ssm.StringParameter.valueForStringParameter(this, "/ddblocal/tableArn")
service.taskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({
actions:["dynamodb:PutItem","dynamodb:GetItem"],
resources:[DDB_TABLE_ARN],
effect: iam.Effect.ALLOW
}))
const serviceDNS = new ssm.StringParameter( this,"serviceDNS",{
parameterName: "local-ddb-service-dns",
stringValue: service.loadBalancer.loadBalancerDnsName
})
}