From 2c321fdd478dd485a1440e2e1dd071387ee9a749 Mon Sep 17 00:00:00 2001 From: Vishnu S <58102524+vishnus17@users.noreply.github.com> Date: Thu, 8 Sep 2022 11:53:44 +0530 Subject: [PATCH] added description for AWS CloudFormation, AWS CDK in AWS topics (#287) * add description for AWS CloudFormation, AWS CDK * adding cdk * fixing typo --- topics/aws/README.md | 16 ++++++ topics/aws/exercises/sample_cdk/exercise.md | 7 +++ topics/aws/exercises/sample_cdk/solution.md | 56 +++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 topics/aws/exercises/sample_cdk/exercise.md create mode 100644 topics/aws/exercises/sample_cdk/solution.md diff --git a/topics/aws/README.md b/topics/aws/README.md index ea01c1b..69f8407 100644 --- a/topics/aws/README.md +++ b/topics/aws/README.md @@ -18,6 +18,7 @@ - [Lambda](#lambda) - [Elastic Beanstalk](#elastic-beanstalk) - [CodePipeline](#codepipeline) + - [CDK](#cdk) - [Misc](#misc) - [Questions](#questions) - [Global Infrastructure](#global-infrastructure) @@ -171,6 +172,11 @@ Failover | Route 53 | [Exercise](exercises/route_53_failover/exercise.md) | [Sol |--------|--------|------|----|----| | Basic CI with S3 | CodePipeline & S3 | [Exercise](exercises/basic_s3_ci/exercise.md) | [Solution](exercises/basic_s3_ci/solution.md) | | +### CDK + +|Name|Topic|Objective & Instructions|Solution|Comments| +|--------|--------|------|----|----| +| Sample CDK | CDK | [Exercise](exercises/sample_cdk/exercise.md) | [Solution](exercises/sample_cdk/solution.md) | | ### Misc |Name|Topic|Objective & Instructions|Solution|Comments| @@ -3135,6 +3141,16 @@ Learn more [here](https://aws.amazon.com/codedeploy)
Explain what is CloudFormation
+ +AWS definition: "AWS CloudFormation is a service that helps you model and set up your Amazon Web Services resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS. You create a template that describes all the AWS resources that you want (like Amazon EC2 instances or Amazon RDS DB instances), and CloudFormation takes care of provisioning and configuring those resources for you." +
+ +
+What is AWS CDK?
+ +AWS definition: "The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define cloud infrastructure as code and provision it through AWS CloudFormation. CDK gives the flexibility to use popular programming languages like TypeScript, JavaScript, Python, Java, C# and Go (in Developer Preview) to define your infrastructure, and AWS CDK provides a set of libraries for AWS services that abstract away the need to write raw CloudFormation templates. + +Learn more [here](https://aws.amazon.com/cdk)
### Misc diff --git a/topics/aws/exercises/sample_cdk/exercise.md b/topics/aws/exercises/sample_cdk/exercise.md new file mode 100644 index 0000000..921f5cf --- /dev/null +++ b/topics/aws/exercises/sample_cdk/exercise.md @@ -0,0 +1,7 @@ +### Set up a CDK Project + +Initialize a CDK project and set up files required to build a CDK project. + +## Solution + +Click [here](solution.md) to view the solution. \ No newline at end of file diff --git a/topics/aws/exercises/sample_cdk/solution.md b/topics/aws/exercises/sample_cdk/solution.md new file mode 100644 index 0000000..4285fa6 --- /dev/null +++ b/topics/aws/exercises/sample_cdk/solution.md @@ -0,0 +1,56 @@ +### Set up a CDK Project - Solution + +### Exercise + +Initialize a CDK project and set up files required to build a CDK project. + +### Solution + +#### Initialize a CDK project + +1. Install CDK on your machine by running `npm install -g aws-cdk`. +2. Create a new directory named `sample` for your project and run `cdk init app --language typescript` to initialize a CDK project. You can choose lanugage as csharp, fsharp, go, java, javascript, python or typescript. +3. You would see the following files created in your directory: + 1. `cdk.json`, `tsconfig.json`, `package.json` - These are configuration files that are used to define some global settings for your CDK project. + 2. `bin/sample.ts` - This is the entry point for your CDK project. This file is used to define the stack that you want to create. + 3. `lib/sample-stack.ts` - This is the main file that will contain the code for your CDK project. + +#### Create a Sample lambda function + +1. In `lib/sample-stack.ts` file, add the following code to create a lambda function: + +```typescript +import * as cdk from 'aws-cdk-lib'; +import * as lambda from 'aws-cdk-lib/aws-lambda'; +import { Construct } from 'constructs'; + +export class SampleStack extends cdk.Stack { + constructor(scope: Construct, id: string, props?: cdk.StackProps) { + super(scope, id, props); + + const hello = new lambda.Function(this, 'SampleLambda', { + runtime: lambda.Runtime.NODEJS_14_X, + code: lambda.Code.fromInline('exports.handler = async () => "hello world";'), + handler: 'index.handler' + }); + } +} + +``` + +This will create a sample lambda function that returns "hello world" when invoked. + +#### Bootstrap the CDK project + +Before you deploy your project. You need to bootstrap your project. This will create a CloudFormation stack that will be used to deploy your project. You can bootstrap your project by running `cdk bootstrap`. + +Learn more about bootstrapping [here](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html). + +##### Deploy the Project + +1. Run `npm install` to install all the dependencies for your project whenever you make changes. +2. Run `cdk synth` to synthesize the CloudFormation template for your project. You will see a new file called `cdk.out/CDKToolkit.template.json` that contains the CloudFormation template for your project. +3. Run `cdk diff` to see the changes that will be made to your AWS account. You will see a new stack called `SampleStack` that will create a lambda function and all the changes associated with it. +4. Run `cdk deploy` to deploy your project. You should see a new stack called created in your AWS account under CloudFormation. +5. Go to Lambda console and you will see a new lambda function called `SampleLambda` created in your account. +