|
| 1 | +import * as integ from '@aws-cdk/integ-tests-alpha'; |
| 2 | +import * as cdk from 'aws-cdk-lib'; |
| 3 | +import * as ec2 from 'aws-cdk-lib/aws-ec2'; |
| 4 | +import * as ecs from 'aws-cdk-lib/aws-ecs'; |
| 5 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 6 | +import * as kms from 'aws-cdk-lib/aws-kms'; |
| 7 | +import * as acm from 'aws-cdk-lib/aws-acmpca'; |
| 8 | + |
| 9 | +const app = new cdk.App(); |
| 10 | +const stack = new cdk.Stack(app, 'aws-ecs-integ'); |
| 11 | +const vpc = new ec2.Vpc(stack, 'Vpc'); |
| 12 | +const cluster = new ecs.Cluster(stack, 'EcsCluster', { |
| 13 | + vpc, |
| 14 | + defaultCloudMapNamespace: { |
| 15 | + name: 'api.com', |
| 16 | + useForServiceConnect: true, |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); |
| 21 | + |
| 22 | +const ca = acm.CertificateAuthority.fromCertificateAuthorityArn(stack, 'CA', |
| 23 | + 'arn:aws:acm-pca:us-east-1:1234567890:certificate-authority/xxxxxx', |
| 24 | +); |
| 25 | + |
| 26 | +const kmsKey = new kms.Key(stack, 'KmsKey', { |
| 27 | + enableKeyRotation: true, |
| 28 | +}); |
| 29 | +kmsKey.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY); |
| 30 | + |
| 31 | +taskDefinition.addContainer('container', { |
| 32 | + image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), |
| 33 | + portMappings: [ |
| 34 | + { |
| 35 | + name: 'api', |
| 36 | + containerPort: 80, |
| 37 | + }, |
| 38 | + ], |
| 39 | +}); |
| 40 | + |
| 41 | +const role = new iam.Role(stack, 'Role', { |
| 42 | + assumedBy: new iam.ServicePrincipal('ecs.amazonaws.com'), |
| 43 | + managedPolicies: [ |
| 44 | + iam.ManagedPolicy.fromAwsManagedPolicyName('AWSPrivateCAFullAccess'), |
| 45 | + ], |
| 46 | +}); |
| 47 | +role.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY); |
| 48 | + |
| 49 | +kmsKey.grant(role, 'kms:GenerateDataKeyPair'); |
| 50 | + |
| 51 | +new ecs.FargateService(stack, 'FargateService', { |
| 52 | + cluster, |
| 53 | + taskDefinition, |
| 54 | + serviceConnectConfiguration: { |
| 55 | + services: [ |
| 56 | + { |
| 57 | + portMappingName: 'api', |
| 58 | + dnsName: 'api', |
| 59 | + port: 80, |
| 60 | + tls: { |
| 61 | + awsPcaAuthorityArn: ca.certificateAuthorityArn, |
| 62 | + role, |
| 63 | + kmsKey, |
| 64 | + }, |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +new integ.IntegTest(app, 'aws-ecs-service-connect-tls-configuration', { |
| 71 | + testCases: [stack], |
| 72 | +}); |
| 73 | + |
| 74 | +app.synth(); |
0 commit comments