I'm working on setting up a CI/CD Pipeline with cdk in typescript. I have a very modular stack structure so I'm having a Stage with 3 stacks: LambdasStack, EndpointsStack and TablesStack. As the name suggest I have all my Lambdas in LambdasStack and so on.
For the pipeline I want the following flow:
- Build
- Deploy Stacks for PreProd
- Integration Test
- Destroy Stacks of PreProd
- Manual approval before Prod
- Deploy Stacks for Prod
The PreProd stacks have to be destroyed before the deployment of the Prod stacks because of the unique names of the tables within the TablesStack. And that's what I'm struggling with. My code to destroy them is:
const deletePreProdStacks= new ShellStep('Delete deployed Stacks', {
commands: [
'npm install',
'cdk destroy -f --all'
]
});
With 'cdk destroy -f --all'
the stacks of the stage are not found so they can't be deleted.
How can I solve this problem? Giving the tables autogenerated names can't be the right solution? Or is there an option to overwrite the PreProdStacks to ProdStacks?
I only have access to only one AWS Account. Because I read that having Testing/PreProd stage and Prod stage on different accounts.
Maybe someone has a similar best practice reference for me?
Thanks in advance :)
Edit1: tag update Edit2: added situation about deployment of PreProd & Prod in same account
CodePudding user response:
Well you don't need to destroy your stacks but specify physical names for resources By “environment” (not preferred).
By environment I mean the stage name, e.g. “pre-prod”, “prod”, etc.
The best solution is to deploy your testing & PreProd & Prod stages in difference accounts.
And if you're using CDK popelines, deploying to multiple accounts shoudn't be too much work, the only thing you have to do is bootstrap all the accounts you deploy to to trust the deployment account (review the bootstrapping docs) and set the account IDs in the stages.
CodePudding user response:
I found an ok solution for giving the resources that can't be duplicated different names by adding the stagename in their properties like this:
In the Stack:
const bookingsTable = new dynamodb.Table(this, 'BookingsTable', {
tableName: `${stageTag}-BookingsTable`,
partitionKey: {
name: 'bookingId',
type: dynamodb.AttributeType.STRING
},
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST
});
In the Stage:
const tables = new TablesStack(this, `TablesStack`, {
stageTag: this.stageName,
env: {
account: account,
region: region
}
});
There by I create 2 tables one named PreProd-BookingsTable
and the other Prod-BookingsTable
.
So I don't need to destroy the already deployed stacks