GithubHelp home page GithubHelp logo

tehzwen / cdktf-aws-lambda Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 85 KB

Simple Terraform CDKTF stack using AWS Lambda functions

TypeScript 61.12% JavaScript 38.08% Python 0.79%
terraform aws lambda apigateway cdk cdktf

cdktf-aws-lambda's Introduction

CDKTF-AWS-Lambda

What is it?

  • Simple project using hashicorp CDKTF in TypeScript to create Lambda functions locally (using zip archives) without the need to upload code to S3

How to use it?

  • Configure your AWS credentials
  • Clone this repo
  • yarn or npm i
  • Install CDKTF using npm or yarn
  • cdktf synth -> Synthesizes your resources
  • cdktf validate -> Validates your resources
  • cdktf deploy -> Creates your resources
  • cdktf destroy -> Destroys your existing resources

Docs

LambdaConstruct

  • Construct wrapper for a lambda function. Creates the lambda function, iam role and zip asset automatically

Constructor Properties:

functionName:   string,         // Name of the function, no spaces or symbols
handler:        string,         // function name of handler ie. main.handler, index.handler
path:           string,         // path to the files where handler is contained
region?:        string,         // aws region for this lambda (default is us-west-2)
rolePolicy?:    string,         // iam policy for this lambda, basic by default
runtime:        LambdaRunTime,  // enum for what type of lambda this is
timeout?:       number,         // timeout for the lambda function

Accessible Properties:

Function : LambdaFunction // returns the lambda function
Role : IamRole // returns the iam role for this lambda
Asset: TerraformAsset // returns the zip archive asset for this lambda

Methods:

addApiExecutionPermission(api: Apigatewayv2Api) : void // creates a permission for api to invoke this lambda function

addApiIntegration(api: Apigatewayv2Api, type: IntegrationType): Apigatewayv2Integration // creates an integration for this lambda function and the api. type by default is AWS_PROXY

Enums:

enum IntegrationType {
  AWS_PROXY = "AWS_PROXY",
  HTTP = "HTTP",
  MOCK = "MOCK",
  HTTP_PROXY = "HTTP_PROXY",
  VPC_LINK = "VPC_LINK"
}

enum LambdaRunTime {
  NODEJS = "nodejs",
  NODEJS_12_X = "nodejs12.x",
  NODEJS_14_X = "nodejs14.x",
  JAVA_8 = "java8",
  JAVA_8_AL2 = "java8.al2",
  JAVA_11 = "java11",
  PYTHON_2_7 = "python2.7",
  PYTHON_3_6 = "python3.6",
  PYTHON_3_7 = "python3.7",
  PYTHON_3_8 = "python3.8",
  PYTHON_3_9 = "python3.9",
  DOTNET_CORE_1_0 = "dotnetcore1.0",
  DOTNET_CORE_2_0 = "dotnetcore2.0",
  DOTNET_CORE_2_1 = "dotnetcore2.1",
  DOTNET_CORE_3_1 = "dotnetcore3.1",
  GO_1_X = "go1.x",
  RUBY_2_5 = "ruby2.5",
  RUBY_2_7 = "ruby2.7",
  PROVIDED = "provided",
  PROVIDED_AL_2 = "provided.al2"
}

ApiGatewayConstruct

  • Construct wrapper for an ApiGatewayV2. Creates the api, and can create routes, deployments, and stages.

Constructor Properties:

apiName: string, // name for the apigateway
protocolType: ProtocolType // HTTP or WEBSOCKET

Accessible Properties:

Api: Apigatewayv2Api // returns the apigateway

Methods:

addDeployment(deploymentName: string): Apigatewayv2Deployment // creates a deployment for the apigateway
addStage(props: { name: string, autoDeploy?: boolean }): Apigatewayv2Stage // creates a stage for the api, autodeploy is true by default
addRoute(integration: Apigatewayv2Integration,props: {httpMethod: HttpMethod,path:string, routeName: string}): Apigatewayv2Route // Creates a route for the apigateway

Enums:

enum ProtocolType {
  HTTP = "HTTP",
  WEBSOCKET = "WEBSOCKET"
}

enum HttpMethod {
  ANY = "ANY",
  GET = "GET",
  POST = "POST",
  PUT = "PUT",
  DELETE = "DELETE"
}

Sample Usage:

import { Construct } from "constructs";
import { TerraformStack } from "cdktf";
import {
  AwsProvider,
} from "@cdktf/provider-aws";
import LambdaConstruct, { LambdaRunTime } from "./lambda-construct";
import ApiGatewayConstruct, { HttpMethod, ProtocolType } from './apigateway-construct';

interface ILambdaStackProps {
  region?: string
}

class LambdaStack extends TerraformStack {
  constructor(scope: Construct, name: string, props: ILambdaStackProps = {}) {
    super(scope, name);

    // define resources here
    new AwsProvider(this, 'aws-provider', {
      region: props.region ?? 'us-west-2'
    });

    const nodeLambdaConstruct = new LambdaConstruct(
      this,
      'LambdaTest',
      {
        path: "../handlers/testHandler/lib",
        functionName: 'TestLambda',
        timeout: 90,
        handler: 'index.handler',
        runtime: LambdaRunTime.NODEJS_14_X
      }
    );

    const pythonLambdaConstruct = new LambdaConstruct(
      this,
      'LambdaPythonTest',
      {
        path: "../handlers/testPython",
        functionName: 'TestPythonLambda',
        timeout: 90,
        handler: 'main.handler',
        runtime: LambdaRunTime.PYTHON_3_8,
      }
    );

    const apiConstruct = new ApiGatewayConstruct(
      this,
      {
        apiName: 'TestApi',
        protocolType: ProtocolType.HTTP
      }
    );

    const homeIntegration = pythonLambdaConstruct.addApiIntegration(apiConstruct.Api);
    const rootIntegration = nodeLambdaConstruct.addApiIntegration(apiConstruct.Api);
    apiConstruct.addRoute(rootIntegration, { httpMethod: HttpMethod.GET, path: "/", routeName: "Root" });
    apiConstruct.addRoute(homeIntegration, { httpMethod: HttpMethod.GET, path: "/home", routeName: 'Home' });
    apiConstruct.addStage({ name: 'TestAPIStage' });
  }
}

export default LambdaStack;

cdktf-aws-lambda's People

Contributors

tehzwen 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.