|
| 1 | +### Set up a CDK Project - Solution |
| 2 | + |
| 3 | +### Exercise |
| 4 | + |
| 5 | +Initialize a CDK project and set up files required to build a CDK project. |
| 6 | + |
| 7 | +### Solution |
| 8 | + |
| 9 | +#### Initialize a CDK project |
| 10 | + |
| 11 | +1. Install CDK on your machine by running `npm install -g aws-cdk`. |
| 12 | +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. |
| 13 | +3. You would see the following files created in your directory: |
| 14 | + 1. `cdk.json`, `tsconfig.json`, `package.json` - These are configuration files that are used to define some global settings for your CDK project. |
| 15 | + 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. |
| 16 | + 3. `lib/sample-stack.ts` - This is the main file that will contain the code for your CDK project. |
| 17 | + |
| 18 | +#### Create a Sample lambda function |
| 19 | + |
| 20 | +1. In `lib/sample-stack.ts` file, add the following code to create a lambda function: |
| 21 | + |
| 22 | +```typescript |
| 23 | +import * as cdk from 'aws-cdk-lib'; |
| 24 | +import * as lambda from 'aws-cdk-lib/aws-lambda'; |
| 25 | +import { Construct } from 'constructs'; |
| 26 | + |
| 27 | +export class SampleStack extends cdk.Stack { |
| 28 | + constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
| 29 | + super(scope, id, props); |
| 30 | + |
| 31 | + const hello = new lambda.Function(this, 'SampleLambda', { |
| 32 | + runtime: lambda.Runtime.NODEJS_14_X, |
| 33 | + code: lambda.Code.fromInline('exports.handler = async () => "hello world";'), |
| 34 | + handler: 'index.handler' |
| 35 | + }); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | +This will create a sample lambda function that returns "hello world" when invoked. |
| 42 | + |
| 43 | +#### Bootstrap the CDK project |
| 44 | + |
| 45 | +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`. |
| 46 | + |
| 47 | +Learn more about bootstrapping [here](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html). |
| 48 | + |
| 49 | +##### Deploy the Project |
| 50 | + |
| 51 | +1. Run `npm install` to install all the dependencies for your project whenever you make changes. |
| 52 | +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. |
| 53 | +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. |
| 54 | +4. Run `cdk deploy` to deploy your project. You should see a new stack called created in your AWS account under CloudFormation. |
| 55 | +5. Go to Lambda console and you will see a new lambda function called `SampleLambda` created in your account. |
| 56 | + |
0 commit comments