in spec/fixtures/cdk/single_service/rails-foo-fargate-stack.ts [20:94]
constructor(scope: cdk.App, id: string, props: RailsFooFargateStackProps) {
super(scope, id);
// import resources
const cluster = props.cluster;
const vpc = props.vpc;
// Create secret from SecretsManager
const username = 'RailsFooDBAdminUser';
const secret = new secretsmanager.Secret(this, 'Secret', {
generateSecretString: {
excludePunctuation: true
}
});
const password = secret.secretValue;
// Import DB cluster ParameterGroup
const parameterGroup = rds.ParameterGroup.fromParameterGroupName(
this, 'DBClusterPG', 'aws-rails-provisioner-default-aurora-postgresql');
// Create DB Cluster
const db = new rds.DatabaseCluster(this, 'DBCluster', {
engine: rds.DatabaseClusterEngine.AURORA_POSTGRESQL,
masterUser: {
username: username,
password: password
},
instanceProps: {
instanceType: new ec2.InstanceType('r4.large'),
vpc: vpc,
vpcSubnets: {
subnetType: ec2.SubnetType.ISOLATED
}
},
defaultDatabaseName: 'app_development',
removalPolicy: cdk.RemovalPolicy.RETAIN,
instances: 2,
parameterGroup: parameterGroup
});
const dbUrl = "postgres://" + username + ":" + password + "@" + db.clusterEndpoint.socketAddress + "/app_development";
this.dbUrl = dbUrl;
const asset = new ecr_assets.DockerImageAsset(this, 'ImageAssetBuild', {
directory: '/absolute/path/to/dir/path/to/rails_foo'
});
// compute repo name from asset image
const parts = asset.imageUri.split("@")[0].split("/");
const repoName = parts.slice(1, parts.length).join("/").split(":")[0];
this.repoName = repoName;
const image = ecs.ContainerImage.fromDockerImageAsset(asset);
// Fargate service
const lbFargate = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'LBFargate', {
serviceName: 'RailsFoo',
cluster: cluster,
taskImageOptions: {
image: image,
containerName: 'FargateTaskContainer',
containerPort: 80,
environment: {
'DATABASE_URL': dbUrl,
},
enableLogging: true,
},
memoryLimitMiB: 512,
cpu: 256,
desiredCount: 5,
publicLoadBalancer: true,
assignPublicIp: true
});
db.connections.allowDefaultPortFrom(lbFargate.service, 'From Fargate');
this.db = db;
this.service = lbFargate.service;
}