aws-cdk executors and generators for nx workspace
npm install @flowaccount/nx-aws-cdk- Have an existing nx workspace. For creating this, see nrwl's documentation.
- Add "aws-cdk": "1.114.0", to your package.json and run yarn
``shell`
npm i --save-dev @flowaccount/nx-aws-cdk
// npx nx g @flowaccount/nx-aws-cdk:init -- coming soon
`shell`
pnpm i --save-dev @flowaccount/nx-aws-cdk
// pnpx nx g @flowaccount/nx-aws-cdk:init -- coming soon
`shell`
yarn add --dev @flowaccount/nx-aws-cdk
// npx nx g @flowaccount/nx-aws-cdk:init -- coming soon
Generate my-api, and my-api-test with C# and nunit tests.
`shell`
yarn nx g @flowaccount/nx-aws-cdk:app my-cdk
Run my-cdk Synthesizer locally
`shell`
yarn nx run my-cdk:synth [--configuration=
Deploy your infrastructure
`shell`
yarn nx run my-cdk:deploy [--configuration=
- First we need to create policies
- Then we spin up the load-balancer and target-group together
- Then we spin up the autoscaling group and cluster together
- Then at last we spin up the service onto cluster supplying the target-group ARN for serving to internet through ALB
`
import { Peer, Port } from "aws-cdk-lib/aws-ec2"
import { PlacementConstraint } from "aws-cdk-lib/aws-ecs"
import { ServicePrincipal } from "aws-cdk-lib/aws-iam"
import { AppEnvModel } from '@flowaccount/core/cdk'
const _region = ap-southeast-1AppName
const _appprefix = fadev
const _stage = app-name
const _app = xxxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com/
const _ecr =
export const environment: AppEnvModel = {
appprefix: _appprefix,
stage: _stage,
app: _app,
awsCredentials: {
account: "xxxxxxxxxxxx",
region: _region
},
vpc: {
vpcAttributes: {
vpcId: vpc-xxxxxxxxxxxxxxxxx,${_region}a
availabilityZones: [ , ${_region}b ],subnet-xxxxxxxxxxxxxxxxx
privateSubnetIds: [ , subnet-xxxxxxxxxxxxxxxxx ]${_app}-${_stage}-ecs-sg-stack
}
},
ecs: {
securityGroup: {
name: ,ec2:*
inboudRule: [
{ peer: Peer.anyIpv4(), connection: Port.allTcp() }
]
},
policy: {
statement: {
actions: [
,s3:*
,ecs:CreateCluster
,ecs:DeregisterContainerInstance
,ecs:DiscoverPollEndpoint
,ecs:Poll
,ecs:RegisterContainerInstance
,ecs:StartTelemetrySession
,ecs:UpdateContainerInstancesState
,ecs:Submit*
,ecr:GetAuthorizationToken
,ecr:BatchCheckLayerAvailability
,ecr:GetDownloadUrlForLayer
,ecr:BatchGetImage
,logs:CreateLogGroup
,logs:CreateLogStream
,logs:PutLogEvents
,logs:DescribeLogStreams
,secretsmanager:GetSecretValue
*
],
resources: [
${_app}-${_stage}-cluster-policy
]
},
name: ${_app}-${_stage}-cluster-role
},
role: {
name: ,ec2.amazonaws.com
assumedBy: new ServicePrincipal()*
},
policyAssume: {
statement: {
actions: [
"secretsmanager:GetSecretValue",
"ssm:Get*",
],
resources: [
]
},
name: ${_app}-${_stage}-cluster-policy-assume${_app}-${_stage}-cluster-role-assume
},
roleAssume: {
name: ,ecs-tasks.amazonaws.com
assumedBy: [new ServicePrincipal(), new ServicePrincipal(ec2.amazonaws.com)]${_app}-${_stage}-cluster-taskRole-assume
},
taskRoleAssume: {
name: ,ecs-tasks.amazonaws.com
assumedBy: [new ServicePrincipal(), new ServicePrincipal(ec2.amazonaws.com)]${_app}-${_stage}-cluster-instance-profile
},
instanceProfile: {
name: ${_app}-${_stage}-lt
},
asgList: [
{
launchTemplate: {
name: ,${_app}-${_stage}-asg
instanceType: "t3.micro",
keyName: "fadev",
version: 1
},
asg: {
name: ,ECSTaskExecutionRole
min: "1",
max: "3",
desired: "3",
overrides: [
{
InstanceType: "t3.micro"
},
{
InstanceType: "t3a.micro"
}
],
onDemandBaseCapacity: 0,
onDemandPercentage: 0,
protectionFromScaleIn: false
}
}
],
executionRoleName: , // arn:aws:iam::697698820969:role/${_app}-${_stage}-cluster
clusterName:
},
service: [
{
taskDefinition: {
name: ${_app}-${_stage}-taskdef,${_ecr}:latest-${_stage}
containerDefinitionOptions: {
image: ,${_app}-${_stage}
memoryLimitMiB: 235,
cpu: 512,
hostname: ${_app}-${_stage}-service
},
portMapping: [
{ hostPort: 0, containerPort: 8080 }
]
},
name: ,attribute:ecs.os-type == linux and attribute:ecs.instance-type in [t3.micro, t3a.micro]
desired: 1,
minHealthyPercent: 0,
placementConstraint: [
PlacementConstraint.memberOf()arn:aws:elasticloadbalancing:ap-southeast-1:697698820969:targetgroup/crm-fadev-tg/fcf09968fd4c148c
],
targetGroupArn: ${_app}-storybook-${_stage}-taskdef
},
{
taskDefinition: {
name: ,${_ecr}:latest-storybook-${_stage}
containerDefinitionOptions: {
image: ,${_app}-storybook-${_stage}
memoryLimitMiB: 235,
cpu: 512,
hostname: ${_app}-storybook-${_stage}-service
},
portMapping: [
{ hostPort: 0, containerPort: 8080 }
]
},
name: ,attribute:ecs.os-type == linux and attribute:ecs.instance-type in [t3.micro, t3a.micro]
desired: 1,
minHealthyPercent: 0,
placementConstraint: [
PlacementConstraint.memberOf()arn:aws:elasticloadbalancing:ap-southeast-1:697698820969:targetgroup/storybook-staging-tg/25a353e4b8fd3123
],
targetGroupArn: ${_app}-developer-handbook-${_stage}-taskdef
},
{
taskDefinition: {
name: ,${_ecr}:latest-developer-handbook-${_stage}
containerDefinitionOptions: {
image: ,${_app}-developer-handbook-${_stage}
memoryLimitMiB: 235,
cpu: 512,
hostname: ${_app}-developer-handbook-${_stage}-service
},
portMapping: [
{ hostPort: 0, containerPort: 80 }
]
},
name: ,attribute:ecs.os-type == linux and attribute:ecs.instance-type in [t3.micro, t3a.micro]
desired: 1,
minHealthyPercent: 0,
placementConstraint: [
PlacementConstraint.memberOf()arn:aws:elasticloadbalancing:ap-southeast-1:697698820969:targetgroup/developer-handbook-staging-tg/1239e837cd2fc44d
],
targetGroupArn: ${_app}-${_stage}-stack
}
],
tag: [
{key: "AppStack", value: }``
]
}
add to libs\nx-aws-cdk\src\builders\deploy\deploy.executor.ts
nx: yarn publish-local
workspace: yarn upgrade nx-aws-cdk
#### Version 2.0.4 (2024-10-16)
- Fix create new ECS service with nx18
- Fix deployment script and update core logic for supporting nx18
#### Version 2.0.3 (2024-09-09)
- Update core function to support Node 20
- Update package version for reduced vulnerability