AWS
so what all we need is all these things below
mkdir qa && cd qa
cdk init.
➜ ~ nvm install v16.3.0
Downloading and installing node v16.3.0...
Downloading...
######################################################################## 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v16.3.0 (npm v7.15.1)
➜ ~ nvm use v16.3.0
Now using node v16.3.0 (npm v7.15.1)
➜ ~ npm install -g aws-cdk
added 2 packages, and audited 3 packages in 2s
➜ ~ cd
➜ ~ mkdir QA
➜ ~ cd QA
➜ QA cdk init sample-app --language typescript
Applying project template sample-app for typescript
You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (QaStack)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.
QaStack
The cdk.json file tells the CDK Toolkit how to execute your app.
cdk.json
npm run build
npm run watch
npm run test
cdk deploy
cdk diff
cdk synth
Initializing a new git repository..
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as subs from 'aws-cdk-lib/aws-sns-subscriptions';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { Construct } from 'constructs';
export class QaStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const queue = new sqs.Queue(this, 'QaQueue', {
visibilityTimeout: Duration.seconds(300)
});
const topic = new sns.Topic(this, 'QaTopic');
topic.addSubscription(new subs.SqsSubscription(queue));
}
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { CdkQAStack } from '../lib/qa-stack';
const app = new cdk.App();
new CdkWorkshopStack(app, 'CdkQAStack');
This code loads and instantiates the CdkWorkshopStack class from the lib/qa-stack.ts file. We won’t need to look at this file anymore.
The main stack
Open up lib/qa-stack.ts. This is where the meat of our application is:
import * as cdk from 'aws-cdk-lib';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as subs from 'aws-cdk-lib/aws-sns-subscriptions';
import * as sqs from 'aws-cdk-lib/aws-sqs';
export class CdkQAStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const queue = new sqs.Queue(this, 'CdkWorkshopQueue', {
visibilityTimeout: cdk.Duration.seconds(300)
});
const topic = new sns.Topic(this, 'CdkWorkshopTopic');
topic.addSubscription(new subs.SqsSubscription(queue));
}
}
As you can see, our app was created with a sample CDK stack (CdkWorkshopStack).
The stack includes::
...
The first time you deploy an AWS CDK app into an environment (account/region), you can install a “bootstrap stack”. This stack includes resources that are used in the toolkit’s operation. For example, the stack includes an S3 bucket that is used to store templates and assets during the deployment process.
You can use the cdk bootstrap command to install the bootstrap stack into an environment:
cdk bootstrap
cdk deploy
You should see a warning like the following:
This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:
IAM Statement Changes
(NOTE: There may be security-related changes not in this list. See)
Do you wish to deploy these changes (y/n)?Y
CdkWorkshopStack: deploying...
CdkWorkshopStack: creating CloudFormation changeset...
✅ CdkWorkshopStack
Stack ARN:
arn:aws:cloudformation:REGION:ACCOUNT-ID:stack/CdkWorkshopStack/STACK-ID
The CloudFormation Console
CDK apps are deployed through AWS CloudFormation. Each CDK stack maps 1:1 with CloudFormation stack.
This means that you can use the AWS CloudFormation console in order to manage your stacks.
Let’s take a look at the AWS CloudFormation console.
Just go to AWS cloudfromation and check console
Open lib/QA-stack.ts and clean it up. Eventually it should look like this:
import * as cdk from 'aws-cdk-lib';
export class CdkWorkshopStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// nothing here!
}
}
Now that we modified our stack’s contents, we can ask the toolkit to show us the difference between our CDK app and what’s currently deployed. This is a safe way to check what will happen once we run cdk deploy and is always good practice:
and now wecan trigger cdk deploy
- [-] AWS::SQS::Queue CdkWorkshopQueue50D9D426 destroy
- [-] AWS::SQS::QueuePolicy CdkWorkshopQueuePolicyAF2494A5 destroy
- [-] AWS::SNS::Topic CdkWorkshopTopicD368A42F destroy
- [-] AWS::SNS::Subscription CdkWorkshopTopicCdkWorkshopQueueSubscription88D211C7 destroy
Conclusion
This Blog was about getting started Now we should be able to deploy any application from our vscode simple editor using AWS SDK, we just need AWS Profile with User account and aws-cdk installed on system
In coming section i will cover some more advance examples using AWS-CDK