Skip to content

Commit 9888efb

Browse files
committed
feat(cdk): fixes for base - app compatibility
1 parent b0820a3 commit 9888efb

File tree

7 files changed

+84
-15
lines changed

7 files changed

+84
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ad-hoc-base-diff:
66
cdk diff --app='./lib/examples/ad-hoc/index.js' -e ExampleAdHocBaseStack
77

88
ad-hoc-base-deploy:
9-
cdk deploy --verbose --app='./lib/examples/ad-hoc/index.js' -e ExampleAdHocBaseStack
9+
cdk deploy --app='./lib/examples/ad-hoc/index.js' -e ExampleAdHocBaseStack
1010

1111
ad-hoc-base-deploy-approve:
1212
cdk deploy --app='./lib/examples/ad-hoc/index.js' --require-approval never -e ExampleAdHocBaseStack

packages/ecs-run-task/action.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: 'Action for running an ECS task in a GHA workflow'
2+
description: 'Action for running ECS task'
3+
author: 'Brian Caffey'
4+
inputs:
5+
BASE_ENV:
6+
required: true
7+
description: 'Base env name (e.g. dev)'
8+
APP_ENV:
9+
required: true
10+
description: 'App env name (e.g. alpha)'
11+
VERSION:
12+
required: true
13+
description: 'Application version git tag (e.g. v1.2.3)'
14+
ECR_REPO:
15+
required: true
16+
description: 'ECR repo to use'
17+
CONTAINER_NAME:
18+
required: true
19+
description: 'Name of the container to update'
20+
AWS_REGION:
21+
required: false
22+
description: 'AWS Region'
23+
default: 'us-east-1'
24+
25+
# Trigger / Inputs
26+
runs:
27+
using: "composite"
28+
steps:
29+
# Note: this assumes that your ECR repo lives in the same AWS account as your ECS cluster
30+
- name: Get current AWS Account
31+
id: get-aws-account
32+
shell: bash
33+
run: |
34+
AWS_ACCOUNT_ID=$(aws sts get-caller-identity | jq -r .Account)
35+
echo "AWS_ACCOUNT_ID=$AWS_ACCOUNT_ID" >> $GITHUB_ENV
36+
37+
- name: Download existing task definition
38+
id: download-task-definition
39+
shell: bash
40+
run: |
41+
aws ecs describe-task-definition \
42+
--task-definition ${{ env.FULL_TASK_NAME }} \
43+
| jq '.taskDefinition' > task-definition.json
44+
45+
- name: Render new task definition
46+
id: render-new-task-definition
47+
uses: aws-actions/amazon-ecs-render-task-definition@v1
48+
with:
49+
task-definition: task-definition.json
50+
container-name: ${{ inputs.CONTAINER_NAME }}
51+
image: ${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.${{ inputs.AWS_REGION}}.amazonaws.com/${{ inputs.ECR_REPO }}:${{ inputs.VERSION }}
52+
53+
- name: Deploy new task definition
54+
id: deploy-new-task-definition
55+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
56+
with:
57+
cluster: ${{ inputs.APP_ENV }}-cluster
58+
service: ${{ inputs.APP_ENV }}-${{ inputs.CONTAINER_NAME }}
59+
task-definition: ${{ steps.render-new-task-definition.outputs.task-definition }}
60+

src/constructs/ad-hoc/app/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export class AdHocApp extends Construct {
7979
// define ecsTaskRole and taskExecutionRole for ECS
8080
const ecsRoles = new EcsRoles(scope, 'EcsRoles');
8181

82+
// allow the task role to read and write to the bucket
83+
props.assetsBucket.grantReadWrite(ecsRoles.ecsTaskRole);
84+
8285
// Route53
8386
const hostedZone = HostedZone.fromLookup(this, 'HostedZone', { domainName: props.domainName });
8487
const cnameRecord = new CnameRecord(this, 'CnameApiRecord', {
@@ -159,7 +162,7 @@ export class AdHocApp extends Construct {
159162
// scheduler service
160163

161164
// management command task definition
162-
const backendUpdateTask = new ManagementCommandTask(this, 'BackendUpdateTask', {
165+
const backendUpdateTask = new ManagementCommandTask(this, 'update', {
163166
cluster,
164167
environmentVariables,
165168
vpc: props.vpc,
@@ -168,7 +171,7 @@ export class AdHocApp extends Construct {
168171
executionRole: ecsRoles.taskExecutionRole,
169172
image: backendImage,
170173
command: ['python', 'manage.py', 'pre_update'],
171-
name: 'backendUpdate',
174+
name: 'update',
172175
});
173176

174177
// worker service

src/constructs/ad-hoc/base/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { DatabaseInstance } from 'aws-cdk-lib/aws-rds';
55
import { Bucket } from 'aws-cdk-lib/aws-s3';
66
import { Construct } from 'constructs';
77
import { AlbResources } from '../../internal/alb';
8-
import { BastionHostResources } from '../../internal/bastion';
98
import { ElastiCacheCluster } from '../../internal/ec';
109
import { RdsInstance } from '../../internal/rds';
1110
import { SecurityGroupResources } from '../../internal/sg';
@@ -40,6 +39,12 @@ export class AdHocBase extends Construct {
4039
const assetsBucket = new Bucket(scope, 'AssetsBucket', {
4140
bucketName: `${props.domainName.replace('.', '-')}-${stackName}-assets-bucket`,
4241
removalPolicy: RemovalPolicy.DESTROY,
42+
blockPublicAccess: {
43+
blockPublicAcls: false,
44+
blockPublicPolicy: false,
45+
ignorePublicAcls: false,
46+
restrictPublicBuckets: false,
47+
},
4348
autoDeleteObjects: true,
4449
});
4550
this.assetsBucket = assetsBucket;
@@ -64,7 +69,6 @@ export class AdHocBase extends Construct {
6469
dbSecretName: this.node.tryGetContext('config')?.dbSecretName ?? 'DB_SECRET_NAME',
6570
});
6671
this.databaseInstance = rdsInstance.rdsInstance;
67-
const { dbInstanceEndpointAddress } = rdsInstance.rdsInstance;
6872

6973
// elasticache cluster
7074
const elastiCacheCluster = new ElastiCacheCluster(this, 'ElastiCacheCluster', {
@@ -75,13 +79,5 @@ export class AdHocBase extends Construct {
7579
// get the elasticache cluster hostname
7680
this.elastiCacheHostname = elastiCacheCluster.elastiCacheHost;
7781

78-
// TODO: is this needed?
79-
new BastionHostResources(this, 'BastionHostResources', {
80-
appSecurityGroup,
81-
vpc: this.vpc,
82-
rdsAddress: dbInstanceEndpointAddress,
83-
instanceClass: this.node.tryGetContext('config').instanceClass,
84-
// instanceType: this.node.tryGetContext('config').instanceType,
85-
});
8682
}
8783
}

src/constructs/internal/sg/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import { Stack } from 'aws-cdk-lib';
1+
import { Stack, Tags } from 'aws-cdk-lib';
22
import { IVpc, Peer, Port, SecurityGroup } from 'aws-cdk-lib/aws-ec2';
33
import { Construct } from 'constructs';
44

@@ -25,10 +25,16 @@ export class SecurityGroupResources extends Construct {
2525
albSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(443), 'HTTPS');
2626
albSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(80), 'HTTP');
2727

28+
const appSgName = `${Stack.of(this).stackName}-app-sg`;
29+
2830
// create application security group
2931
const appSecurityGroup = new SecurityGroup(scope, 'AppSecurityGroup', {
32+
securityGroupName: appSgName,
3033
vpc: props.vpc,
3134
});
35+
36+
Tags.of(appSecurityGroup).add('Name', `${Stack.of(this).stackName}-app-sg`);
37+
3238
appSecurityGroup.connections.allowFrom(appSecurityGroup, Port.allTcp());
3339

3440
this.appSecurityGroup = appSecurityGroup;

src/constructs/internal/vpc/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Stack } from 'aws-cdk-lib';
1+
import { Stack, Tags } from 'aws-cdk-lib';
22
import { IVpc, SubnetType, Vpc } from 'aws-cdk-lib/aws-ec2';
33

44
import { Construct } from 'constructs';
@@ -28,5 +28,8 @@ export class ApplicationVpc extends Construct {
2828
],
2929
});
3030
this.vpc = vpc;
31+
32+
// having trouble making sure the VPC resources are getting tagged correctly
33+
Tags.of(vpc).add('base-env', Stack.of(this).stackName);
3134
}
3235
}

src/examples/ad-hoc/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ const addHocApp = new AdHocApp(appStack, 'AdHocApp', {
4545
* Add tagging for this construct and all child constructs
4646
*/
4747
Tags.of(adHocBase).add('base-env', adHocBaseEnvName);
48+
Tags.of(adHocBase).add('ad-hoc', 'true');
4849
Tags.of(addHocApp).add('app-env', adHocAppEnvName);

0 commit comments

Comments
 (0)