Convert Figma logo to code with AI

aws-samples logoaws-cdk-examples

Example projects using the AWS CDK

5,101
2,145
5,101
81

Top Related Projects

The AWS Solutions Constructs Library is an open-source extension of the AWS Cloud Development Kit (AWS CDK) that provides multi-service, well-architected patterns for quickly defining solutions

Serverless patterns. Learn more at the website: https://serverlessland.com/patterns.

11,501

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code

Quick Overview

The aws-samples/aws-cdk-examples repository is a collection of example projects and code snippets demonstrating how to use the AWS Cloud Development Kit (CDK) to define cloud infrastructure as code. It covers various AWS services and common architectural patterns, providing developers with practical examples to learn from and adapt for their own projects.

Pros

  • Comprehensive collection of examples covering many AWS services and use cases
  • Well-organized structure with examples categorized by programming language
  • Regularly updated to reflect the latest CDK features and best practices
  • Includes both simple and complex examples, catering to different skill levels

Cons

  • Some examples may become outdated as AWS services and CDK evolve
  • Not all examples are available in every supported programming language
  • Limited documentation within some example projects
  • May require additional AWS knowledge to fully understand and implement certain examples

Code Examples

Here are a few short code examples from the repository:

  1. Creating an S3 bucket with CDK (TypeScript):
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new s3.Bucket(this, 'MyBucket', {
      versioned: true,
      encryption: s3.BucketEncryption.S3_MANAGED,
    });
  }
}
  1. Creating a Lambda function with CDK (Python):
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
)
from constructs import Construct

class MyStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        _lambda.Function(
            self, 'MyLambdaFunction',
            runtime=_lambda.Runtime.PYTHON_3_9,
            handler='index.handler',
            code=_lambda.Code.from_asset('lambda')
        )
  1. Creating an API Gateway with CDK (JavaScript):
const cdk = require('aws-cdk-lib');
const apigateway = require('aws-cdk-lib/aws-apigateway');

class MyStack extends cdk.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    new apigateway.RestApi(this, 'MyApi', {
      restApiName: 'My API Service',
      description: 'This service serves as an example API.',
    });
  }
}

Getting Started

To get started with the AWS CDK examples:

  1. Clone the repository:

    git clone https://github.com/aws-samples/aws-cdk-examples.git
    
  2. Navigate to the desired example directory:

    cd aws-cdk-examples/typescript/my-widget-service
    
  3. Install dependencies:

    npm install
    
  4. Deploy the stack:

    cdk deploy
    

Note: Make sure you have the AWS CDK CLI installed and configured with your AWS credentials before deploying.

Competitor Comparisons

The AWS Solutions Constructs Library is an open-source extension of the AWS Cloud Development Kit (AWS CDK) that provides multi-service, well-architected patterns for quickly defining solutions

Pros of aws-solutions-constructs

  • Provides higher-level constructs for common architectural patterns
  • Focuses on best practices and well-architected solutions
  • Offers a more opinionated approach to infrastructure design

Cons of aws-solutions-constructs

  • Less flexibility compared to raw CDK constructs
  • May not cover all possible use cases or custom requirements
  • Steeper learning curve for understanding the pre-built constructs

Code Comparison

aws-cdk-examples:

const bucket = new s3.Bucket(this, 'MyBucket');
const table = new dynamodb.Table(this, 'MyTable', {
  partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }
});

aws-solutions-constructs:

new S3ToDynamoDB(this, 'S3ToDynamoDBPattern', {
  bucketProps: {
    encryption: s3.BucketEncryption.S3_MANAGED
  },
  tableProps: {
    partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }
  }
});

The aws-solutions-constructs example demonstrates a higher-level construct that combines S3 and DynamoDB resources with pre-configured best practices, while the aws-cdk-examples shows individual resource creation with more granular control.

Serverless patterns. Learn more at the website: https://serverlessland.com/patterns.

Pros of serverless-patterns

  • Focuses specifically on serverless architectures, providing targeted examples
  • Includes a wider variety of AWS services and integration patterns
  • Offers examples in multiple languages and frameworks (CloudFormation, CDK, SAM, Terraform)

Cons of serverless-patterns

  • May not cover as many general AWS infrastructure scenarios
  • Could be overwhelming for beginners due to the large number of patterns
  • Less emphasis on CDK-specific implementations

Code Comparison

serverless-patterns (CloudFormation example):

AWSTemplateFormatVersion: '2010-09-09'
Description: Lambda to SQS pattern
Resources:
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler

aws-cdk-examples (CDK example):

import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

The serverless-patterns repository provides a broader range of serverless-focused examples across multiple languages and frameworks, making it valuable for exploring diverse serverless architectures. However, aws-cdk-examples offers more comprehensive CDK-specific implementations and covers a wider range of AWS services beyond serverless, which may be beneficial for general AWS infrastructure development using CDK.

11,501

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code

Pros of aws-cdk

  • Official AWS CDK repository with comprehensive documentation
  • Actively maintained with frequent updates and bug fixes
  • Extensive community support and contributions

Cons of aws-cdk

  • Larger codebase, potentially more complex for beginners
  • Focuses on core functionality rather than specific examples
  • May require more setup and configuration for simple use cases

Code Comparison

aws-cdk:

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
new s3.Bucket(stack, 'MyBucket');

aws-cdk-examples:

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

class MyStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    new s3.Bucket(this, 'MyBucket');
  }
}

The aws-cdk repository provides the core functionality and building blocks for AWS CDK, while aws-cdk-examples offers practical examples and use cases. The code comparison shows that aws-cdk-examples tends to use more structured, class-based approaches in its examples, which can be helpful for understanding best practices and real-world implementations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

AWS CDK Examples

This repository contains a set of example projects for the AWS Cloud Development Kit.

Table of Contents

  1. About this Repo
  2. Examples
  3. Learning Resources
  4. Additional Examples
  5. License

About this Repo

This repo is our official list of CDK example code. The repo is subdivided into sections for each language (see "Examples"). Each language has its own subsection of examples with the ultimate aim of complete language parity (same subset of examples exist in each language). These examples each provide a demonstration of a common service implementation, or infrastructure pattern that could be useful in your use of the CDK for building your own infrastructure.

We welcome contributions to this repo in the form of fixes to existing examples or addition of new examples. For more information on contributing, please see the CONTRIBUTING guide.

This is considered an intermediate learning resource and should typically be referenced after reading the Developer Guide or CDK Workshop (please see Learning Resources for more information on these resources).

Examples

This repo contains examples in each language supported by the CDK. Some languages are fully supported by JSII, but as additional languages are added, you will see those marked as Developer Preview. You can find the examples for each of those languages at the following links:

LanguageJSII Language-Stability
Typescript ExamplesStable
Python ExamplesStable
.NET ExamplesStable
Java ExamplesStable
Go ExamplesStable

Learning Resources

While this is an excellent learning resource for the CDK, there are other resources that can be referenced to assist with your learning/development process.

Official Resources

Unofficial/Community Resources

If you have created a CDK learning resource and would like it to be listed here, please read the related CONTRIBUTING section for more info.

Additional Examples

The examples listed below are larger examples hosted in their own repositories that demonstrate more complex or complete CDK applications.

If you would like your repo to be listed here, please read the CONTRIBUTING guide for more details.

ExampleDescriptionOwner
aws-cdk-changelogs-demoA full serverless Node.js application stack deployed using CDK. It uses AWS Lambda, AWS Fargate, DynamoDB, Elasticache, S3, and CloudFront.AWS

License

This library is licensed under the Apache 2.0 License.