Create Lambda Function and REST API using AWS CDK
AWS CDK is a great framework to build and configure AWS resources. You can create new resources as well as import existing resources to your CDK stack. Let’s understand how to provision a Lambda function and corresponding API endpoint using CDK
If you haven’t setup CDK on your system yet then first thing you need to do is to install and configure AWS credentials.
INSTALLATION
Download node and python from following URLs and install them. I have used Python 3.7 for my CDK project
Node JS -> https://nodejs.org/en/download/
Python -> https://www.python.org/downloads/
Checkout npm and Python versions:
$ npm --version
6.13.7$ python --version
Python 3.7.4
Next, install CDK using npm command and then checkout CDK version.
$ npm install aws-cdk -g
$ cdk --version
1.42.0 (build 3b64241
IMP: It is recommended to update CDK regularly because AWS releases the new builds frequently. Regular updates ensure that you get all the supported services and features released by AWS
Now let’s configure AWS credentials which you can use with CDK CLI. You need to have AWS CLI installed, which can be downloaded from here.
$ aws configure --profile cdk
AWS Access Key ID [None]: YOUR_ACCESS_KEY
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
Default region name [None]: REGION
Default output format [None]:
Above process will create and configure a new AWS profile to store the credentials. It comes really handy when you have to work on multiple AWS accounts.
CDK Project Structure
At bare minimum you need to create following project structure to run AWS CDK application. I prefer to create the structure by myself instead of initializing the sample app.
cdkapp/
- app.py
- cdk.json
- requirements.txt
- stacks/
- lambda_stack.py
- lambda
- hello.py
Let’s breakdown above project structure:
- cdkapp is the application name which is the root of the project
- app.py is the main entry point of our project
- requirements.txt contains CDK modules that you need to install to support your project
- stacks directory contains definition of all stacks that you want to define
- lambda_stack.py contains definition of Lambda stack
- lambda directory contains definition of Lambda functions
Below is the definition of each file mentioned above.
lambda_stack.py
from aws_cdk import (
aws_lambda as lb,
aws_apigateway as apigw,
core
)
class LambdaStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs) lambda_function = lb.Function(self, 'helloworldfunction',
runtime=lb.Runtime.PYTHON_3_8,
code=lb.Code.asset('lambda'),
handler='hello.handler'
)
api_gateway = apigw.LambdaRestApi(self,'helloworld',
handler=lambda_function,
rest_api_name='mylambdaapi'
)
cdk.json
{
"app": "python app.py"
}
requirements.txt
aws-cdk.core
aws-cdk.aws-lambda
aws-cdk.aws-apigateway
app.py
#!/usr/bin/env python3
from aws_cdk import core
from stacks.lambda_stack import LambdaStackapp = core.App()
lambda_stack = LambdaStack(app,'lambda')app.synth()
Now once you have everything configured, you can run following CDK commands to preview and deploy the changes
$ cdk ls #It lists out all the stacks you have defined in your project$ cdk synth lambda #It will synthesize the code written above in lambda_stack.py to CloudFormation template$ cdk diff lambda --profile cdk #It will output the difference between deployed stack and changes you have made$ cdk deploy lambda --profile cdk #Deploys the Lambda stack
Once the deployment will be completed, it will create a Lambda function and it’s corresponding REST API. CDK will also takes care of provisioning IAM roles and assigning appropriate permissions.
If you want to explore more about provisioning AWS resources using AWS CDK then do checkout my comprehensive online course on Udemy
Course URL with embedded coupon code: https://bit.ly/2TR0sTQ
Course offers:
Get the course for $12.99 for limited time.
Top 20 students who finish the course first will get an Amazon gift card worth $12.99