Giter Club home page Giter Club logo

aws-cdk's Introduction

Infrastructure as Code: How to Deploy an Python Lambda Function using the AWS Cloud Development Kit (CDK)

We can use the Python version of CDK to allow us to manage projects, bootstrap or migrate projects to different accounts.

In this tutorial, we re-created the Python Lambda function and API Gateway from the previous tutorial using "infrastructure-as-code" (with AWS CDK) and deployed it.

Infrastructure as code

All of the resources we created last time including the Lambda and Gateway function we refer to as Infrastructure

Luckily there are tools that exist which allow us to create, deploy and maintain all of this infrastructure using code.

Most of the time this infrastructure is defined using markup formatting language such as json or yaml

But there are frameworks that allow us to do this in a scripting language such as Python or Typescript which is easier because you have thigns like type-referencing, IDE completion, loops, conditions, etc.

This is also great because instead of having to manually click things in our console, we end up with a bunch of code that we can use to deploy our infrastructure with.

Popular Infrastructure-as-Code (IAC) Frameworks include Terraform, Ansible, Pulumi and AWS CDK. This makes the most sense since we are already on a AWS stack.

AWS CDK

Architecture:

CDK will allow us to find and deploy our Lambda function & API Gateway integration as code.

Prerequisites

    1. AWS CLI
    2. NodeJS (with npm)

Once installed, try the following commands so that everything is setup to be working properly:

aws sts get-caller-identity

^ this prooves that you have AWS CLI configured and that you are using the correct account

npm --version

to check that you have the right node version installed

Install and bootstrap CDK

Install the latest cdk version:

npm install -g aws-cdk@latest

check that it is working by typing:

cdk --version

Next run a boostrap CDK

cdk bootstrap aws://{account#}/{region}

Once you run this it will create something called a cloud formation stack in your account for that region. A cloud formation stack is essentially logical grouping for your resources.

Create your project

Create a new folder:

mkdir infrastructure

Navigate to that folder:

cd infrastructure

Type:

cdk init --language python

You will see that the last command made a bunch of files for us. When we deploy these files it will create another cloud formation stack in our account.

Create the Lambda function

Now we have to add the Lambda function and API Gateway from the first tutorial to our resources stack.

First, add the below to the requirements.txt file:

aws-cdk.core
aws-cdk.aws_lambda

Install the modules using this command:

pip install -r requirements.txt

Once the dependencies have been installed we can move over our function from the AWS Lambda->functions console into a folder within /infrastructure called /compute.

In the infrastructure_stack.py file, import the lastest aws_lambda package from the documentation and create a new fucntion object:

from aws_cdk import aws_lambda
# The code that defines your stack goes here
 random_drink_function =aws_lambda.Function(
            self, # the logical resource that will be the owner of this lambda function
            id="RandomDrinkFunctionV2", # give it a random name
            code=aws_lambda.Code.from_asset("./compute/"), # where Lambda finds the code, pointed to our compute folder
            handler="random_drink_lambda_handler", # specified handler, which is the python function and package we want Lambda to use when it is triggered
            runtime=aws_lambda.Runtime.PYTHON_3_12 # tell it what runtime to use
            )

Finally run:

cdk deploy

Head to the AWS Lambda function console to see if your new function with the code is stored.

Create the API integration

Now that we have our Lambda function in the cloud, we can add our API integration

Update your infrastructure_stack.py file to include gatewayv2"

from aws_cdk import (
    aws_apigatewayv2 as apigwv2,
    aws_apigatewayv2_integrations as integrations,
)

Re-run:

pip install -r requirements.txt

Run:

cdk deploy

Go back to AWS Kambda -> Functions and you should find the API Gateway trigger already connected to your Lambda function. When you open the URL provided it should work.

Full Tutorial video

AWS CDK Tutorial: Deploy a Python Lambda Function using AWS

aws-cdk's People

Contributors

markbuckle avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.