First, let us understand "How to Get Started with AWS CDK" to work with the current project:
CDK (Cloud Development Kit) is a framework for defining cloud infrastructure as code and provisioning it through Cloud Formation templates. You can connect your AWS resources together (even across Stacks) and grant permissions using simple, intent-oriented API's. In short, it is defined as "cloud infrastructure as code", i.e., provisioning cloud infrastructure using programming languages. CDK supports: TypeScript, JavaScript,Python, Java, C#/.Net, and Go languages For more information and documentation about CDK, refer to: "https://docs.aws.amazon.com/cdk/v2/guide/home.html"
Getting started with AWS CDK includes the following steps:
Before you begin, make sure you have the following prerequisites in place:
An AWS account: You'll need an AWS account to use AWS CDK.
AWS CLI: Install the AWS Command Line Interface (CLI) and configure it with your AWS credentials. You can download and install it from the AWS CLI website: "https://aws.amazon.com/cli/"
Node.js and npm: AWS CDK uses Node.js for its CLI. You can download Node.js from the official website: "https://nodejs.org/"
Open a terminal or command prompt and install AWS CDK globally using npm:
npm install -g aws-cdk
Verify that AWS CDK is installed correctly (version) by running:
cdk --version
AWS CDK uses the AWS CLI's configuration to access your AWS account. Make sure you've already configured the AWS CLI using aws configure
. If not, run:
aws configure
You'll be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and output format. For more help on CLI, refer to: "https://docs.aws.amazon.com/pdfs/cli/latest/userguide/aws-cli.pdf#cli-chap-getting-started"
Now, let's create our first CDK app. We will use the following command to get started:
cdk init app --language=python
Replace Python with your preferred programming language if you prefer a different one (e.g., typescript
, javascript
, etc.).
Change your current working directory to the newly created CDK app folder:
cd <your-app-name>
AWS CDK projects consist of one or more "stacks." A stack is a set of AWS resources that are created and managed together. You can define your stacks in the lib/<app-name>-stack.ts
(or .js or .py, depending on your chosen language) file. So,an example Python CDK stack definition in lib/<app-name>-stack.py
:
from aws_cdk import core
class MyCdkStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Define AWS resources here
# For example, create an S3 bucket:
from aws_cdk import aws_s3 as s3
bucket = s3.Bucket(
self,
"MyCdkBucket",
versioned=True,
)
Deploy your CDK stack to create AWS resources. Run the following command from your CDK app's root directory:
cdk deploy
CDK will provide a preview of the changes to be made and ask for confirmation. Type 'y' to proceed.
You can now explore your AWS resources in the AWS Management Console or manage them programmatically using CDK.
If you want to remove the resources created by your CDK stack, run:
cdk destroy
This will delete the AWS resources but won't remove the CDK app itself.
And Boom! You've created your first AWS CDK app and deployed a stack. You can continue to define more stacks, integrate with other AWS services, and use CDK to manage your infrastructure as code (IaC).
- API reference: "https://docs.aws.amazon.com/cdk/api/v2"
- CDK workshop: "https://cdkworkshop.com/"
- Community hub: "https://cdk.dev/" - including a slack channel
- AWS CDK examples: "https://github.com/aws-samples/aws-cdk-examples"
- CDK patterns: "https://cdkpatterns.com/"
- Aweome CDK: "https://github.com/kolomied/awesome-cdk
- AWS solutions constructs: "http://aws.amazon.com/solutions/constructs/
- AWS developer blog: "https://aws.amazon.com/blogs/developer/category/developer-tools/aws-cloud-development-kit"
- Stack overflow: "https://stackoverflow.com/questions/tagged/aws-cdk"
- Github repository: "https://github.com/awslabs/aws-cdk" Issues: "https://github.com/awslabs/aws-cdk/issues" Examples: "https://github.com/aws-samples/aws-cdk-examples" Documentation store: "https://github.com/awsdocs/aws-cdk-guide" Licence: "https://github.com/awslabs/aws-cdk/blob/main/LICENSE" Releases: "https://github.com/awslabs/aws-cdk/releases"
- AWS CDK sample for Cloud 9: "https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-cdk.html"
- AWS Cloud Formation concepts: "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-concepts.html"
- AWS Glossary: "https://docs.aws.amazon.com/general/latest/gr/glos-chap.html"
These tools can work with the AWS CDK to simplify serverless application development and deployment.
- AWS Serverless application model: "http://aws.amazon.com/serverless/sam/"
- A python serverless microframework: "https://github.com/aws/chalice"