• Stars
    star
    330
  • Rank 127,357 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

lambroll is a minimal deployment tool for AWS Lambda.

lambroll

lambroll is a minimal deployment tool for AWS Lambda.

lambroll does,

  • Create a function.
  • Create a Zip archive from local directory.
  • Update function code / configuration / tags / aliases.
  • Rollback a function to previous version.
  • Invoke a function with payloads.
  • Manage function versions.

That's all.

lambroll does not,

  • Manage resources related to the Lambda function.
    • e.g. IAM Role, function triggers, API Gateway, etc.
  • Build native binaries or extensions for Linux (AWS Lambda running environment).

When you hope to manage these resources, we recommend other deployment tools (AWS SAM, Serverless Framework, etc.).

Install

Homebrew (macOS and Linux)

$ brew install fujiwara/tap/lambroll

Binary packages

Releases

CircleCI Orb

https://circleci.com/orbs/registry/orb/fujiwara/lambroll

version: 2.1
orbs:
  lambroll: fujiwara/[email protected]
jobs:
  deloy:
    docker:
      - image: cimg/base
    steps:
      - checkout
      - lambroll/install:
          version: v0.12.2
      - run:
          command: |
            lambroll deploy

GitHub Actions

Action fujiwara/lambroll@v0 installs lambroll binary for Linux into /usr/local/bin. This action runs install only.

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: fujiwara/lambroll@v0
        with:
          version: v0.12.2
      - run: |
          lambroll deploy

Quick start

Try migrate your existing Lambda function hello.

$ mkdir hello
$ cd hello
$ lambroll init --function-name hello --download
2019/10/26 01:19:23 [info] function hello found
2019/10/26 01:19:23 [info] downloading function.zip
2019/10/26 01:19:23 [info] creating function.json
2019/10/26 01:19:23 [info] completed

$ unzip -l function.zip
Archive:  function.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      408  10-26-2019 00:30   index.js
---------                     -------
      408                     1 file

$ unzip function.zip
Archive:  function.zip
 extracting: index.js

$ rm function.zip

See or edit function.json or index.js.

Now you can deploy hello fuction using lambroll deploy.

$ lambroll deploy
2019/10/26 01:24:52 [info] starting deploy function hello
2019/10/26 01:24:53 [info] creating zip archive from .
2019/10/26 01:24:53 [info] zip archive wrote 1042 bytes
2019/10/26 01:24:53 [info] updating function configuration
2019/10/26 01:24:53 [info] updating function code hello
2019/10/26 01:24:53 [info] completed

Usage

usage: lambroll [<flags>] <command> [<args> ...]

Flags:
  --help                      Show context-sensitive help (also try --help-long and --help-man).
  --log-level=info            log level (trace, debug, info, warn, error)
  --function="function.json"  Function file path
  --color                     enable colored output
  --profile=""                AWS credential profile name
  --region=""                 AWS region
  --tfstate=""                URL to terraform.tfstate
  --prefixed-tfstate=PREFIX=URL ...
                              key value pair of the prefix for template function name and URL to terraform.tfstate
  --endpoint=""               AWS API Lambda Endpoint
  --envfile=ENVFILE ...       environment files
  --ext-str=EXT-STR ...       external string values for Jsonnet
  --ext-code=EXT-CODE ...     external code values for Jsonnet

Commands:
  help [<command>...]
    Show help.

  version
    show version

  init --function-name=FUNCTION-NAME [<flags>]
    init function.json

  list
    list functions

  deploy [<flags>]
    deploy or create function

  rollback [<flags>]
    rollback function

  delete [<flags>]
    delete function

  invoke [<flags>]
    invoke function

  archive [<flags>]
    archive zip

  logs [<flags>]
    tail logs using `aws logs tail` (aws-cli v2 required)

  diff [<flags>]
    show display diff of function.json compared with latest function

  versions [<flags>]
    manage function versions

Init

lambroll init initialize function.json by existing function.

usage: lambroll init --function-name=FUNCTION-NAME [<flags>]

init function.json

Flags:
  (common flags snipped)
  --function-name=FUNCTION-NAME  Function name for initialize
  --download                     Download function.zip

init creates function.json as a configuration file of the function.

Deploy

usage: lambroll deploy [<flags>]

deploy or create function

Flags:
  (common flags snipped)
  --src="."                   function zip archive or src dir
  --exclude-file=".lambdaignore"
                              exclude file
  --dry-run                   dry run
  --publish                   publish function
  --alias="current"           alias name for publish
  --alias-to-latest           set alias to unpublished $LATEST version
  --skip-archive              skip to create zip archive. requires Code.S3Bucket and Code.S3Key in function definition
  --keep-versions=0           Number of latest versions to keep. Older versions will be deleted. (Optional value: default 0).

deploy works as below.

  • Create a zip archive from --src directory.
    • Excludes files matched (wildcard pattern) in --exclude-file.
  • Create / Update Lambda function
  • Create an alias to the published version when --publish (default).

Deploy container image

lambroll also support to deploy a container image for Lambda.

PackageType=Image and Code.ImageUri are required in function.json.

{
  "FunctionName": "container",
  "MemorySize": 128,
  "Role": "arn:aws:iam::012345678912:role/test_lambda_function",
  "PackageType": "Image",
  "Code": {
    "ImageUri": "012345678912.dkr.ecr.ap-northeast-1.amazonaws.com/lambda/test:latest"
  }
}

Rollback

usage: lambroll rollback [<flags>]

rollback function

Flags:
  (common flags snipped)
  --delete-version            Delete rolled back version
  --dry-run                   dry run

lambroll deploy create/update alias current to the published function version on deploy.

lambroll rollback works as below.

  1. Find previous one version of function.
  2. Update alias current to the previous version.
  3. When --delete-version specified, delete old version of function.

Invoke

usage: lambroll invoke [<flags>]

invoke function

Flags:
  (common flags snipped)
  --async                     invocation type async
  --log-tail                  output tail of log to STDERR
  --qualifier=QUALIFIER       version or alias to invoke

lambroll invoke accepts multiple JSON payloads for invocations from STDIN.

Outputs from function are printed in STDOUT.

$ echo '{"foo":1}{"foo":2}' | lambroll invoke --log-tail
{"success": true, payload{"foo:1}}
2019/10/28 23:16:43 [info] StatusCode:200 ExecutionVersion:$LATEST
START RequestId: 60140e16-018e-41b1-bb46-3f021d4960c0 Version: $LATEST
END RequestId: 60140e16-018e-41b1-bb46-3f021d4960c0
REPORT RequestId: 60140e16-018e-41b1-bb46-3f021d4960c0	Duration: 561.77 ms	Billed Duration: 600 ms	Memory Size: 128 MB	Max Memory Used: 50 MB
{"success": true, payload:{"foo":2}}
2019/10/28 23:16:43 [info] StatusCode:200 ExecutionVersion:$LATEST
START RequestId: dcc584f5-ceaf-4109-b405-8e59ca7ae92f Version: $LATEST
END RequestId: dcc584f5-ceaf-4109-b405-8e59ca7ae92f
REPORT RequestId: dcc584f5-ceaf-4109-b405-8e59ca7ae92f	Duration: 597.87 ms	Billed Duration: 600 ms	Memory Size: 128 MB	Max Memory Used: 50 MB
2019/10/28 23:16:43 [info] completed

function.json

function.json is a definition for Lambda function. JSON structure is based from CreateFunction for Lambda API.

{
  "Architectures": [
    "arm64"
  ],
  "Description": "hello function for {{ must_env `ENV` }}",
  "EphemeralStorage": {
    "Size": 1024
  },
  "Environment": {
    "Variables": {
      "BAR": "baz",
      "FOO": "{{ env `FOO` `default for FOO` }}"
    }
  },
  "FunctionName": "{{ must_env `ENV` }}-hello",
  "FileSystemConfigs": [
    {
      "Arn": "arn:aws:elasticfilesystem:ap-northeast-1:123456789012:access-point/fsap-04fc0858274e7dd9a",
      "LocalMountPath": "/mnt/lambda"
    }
  ],
  "Handler": "index.js",
  "MemorySize": 128,
  "Role": "arn:aws:iam::123456789012:role/hello_lambda_function",
  "Runtime": "nodejs18.x",
  "Tags": {
    "Env": "dev"
  },
  "Timeout": 5,
  "TracingConfig": {
    "Mode": "PassThrough"
  }
}

Tags

When "Tags" key exists in function.json, lambroll set / remove tags to the lambda function at deploy.

{
  // ...
  "Tags": {
    "Env": "dev",
    "Foo": "Bar"
  }
}

When "Tags" key does not exist, lambroll doesn't manage tags. If you hope to remove all tags, set "Tags": {} expressly.

Expand enviroment variables

At reading the file, lambrol evaluates {{ env }} and {{ must_env }} syntax in JSON.

For example,

{{ env `FOO` `default for FOO` }}

Environment variable FOO is expanded here. When FOO is not defined, use default value.

{{ must_env `FOO` }}

Environment variable FOO is expanded. When FOO is not defined, lambroll will panic and abort.

json_escape template function escapes JSON meta characters in string values. This is useful for inject structured values into environment variables.

{
    "Environment": {
        "Variables": {
            "JSON": "{{ env `JSON` | json_escape }}"
        }
    }
}

Enviroment variables from envfile

lambroll --envfile .env1 .env2 reads files named .env1 and .env2 as environment files and export variables in these files.

These files are parsed by hashicorp/go-envparse.

FOO=foo
export BAR="bar"

Lookup resource attributes in tfstate (Terraform state)

When --tfstate option set to an URL to terraform.tfstate, tfstate template function enabled.

For example, define your AWS resources by terraform.

data "aws_iam_role" "lambda" {
  name = "hello_lambda_function"
}

terraform apply creates a terraform.tfstate file.

lambroll --tfstate URL ... enables to lookup resource attributes in the tfstate URL.

{
  "Description": "hello function",
  "FunctionName": "hello",
  "Handler": "index.js",
  "MemorySize": 128,
  "Role": "{{ tfstate `data.aws_iam_role.lambda.arn` }}",
  "Runtime": "nodejs12.x",
  "Timeout": 5,
  "TracingConfig": {
    "Mode": "PassThrough"
  },
  "VpcConfig": {
    "SubnetIds": [
      "{{ tfstate `aws_subnet.lambda['az-a'].id` }}",
      "{{ tfstate `aws_subnet.lambda['az-b'].id` }}"
    ],
    "SecurityGroupIds": [
      "{{ tfstatef `aws_security_group.internal['%s'].id` (must_env `WORLD`) }}"
    ]
  }
}

Likewise, if you have AWS resource definitions spread across multiple tfstate files, you can utilize --prefixed-tfstate option:

e.g.

lambroll --prefixed-tfstate="my_first_=s3://my-bucket/first.tfstate" --prefixed-tfstate="my_second_=s3://my-bucket/second.tfstate" ...

which then exposes additional template functions available like:

{
  "Description": "hello function",
  "Environment": {
    "Variables": {
      "FIRST_VALUE": "{{ my_first_tfstate `data.aws_iam_role.lambda.arn` }}",
      "SECOND_VALUE": "{{ my_second_tfstate `data.aws_iam_role.lambda.arn` }}"
    }
  },
  "rest of the parameters": "..."
}

Jsonnet support for function configuration

lambroll also can read function.jsonnet as Jsonnet format.

{
  FunctionName: 'hello',
  Handler: 'index.handler',
  MemorySize: std.extVar('memorySize'),
  Role: 'arn:aws:iam::%s:role/lambda_role' % [ std.extVar('accountID') ],
  Runtime: 'nodejs14.x',
}
$ lambroll \
    --function function.jsonnet \
    --ext-str accountID=0123456789012 \
    --ext-code memorySize="128 * 4" \
    deploy

.lambdaignore

lambroll will ignore files defined in .lambdaignore file at creating a zip archive.

For example,

# comment

*.zip
*~

For each line in .lambdaignore are evaluated as Go's path/filepath#Match.

Lambda@Edge support

lambroll can deploy Lambda@Edge functions.

Edge functions require two preconditions:

  • --region must set to us-east-1.
  • The IAM Role must be assumed by lambda.amazonaws.com and edgelambda.amazonaws.com both.

Otherwise, it works as usual.

LICENSE

MIT License

Copyright (c) 2019 FUJIWARA Shunichiro

More Repositories

1

stretcher

Deployment tool with consul/serf event notification.
Go
245
star
2

shapeio

Traffic shaper for Golang io.Reader and io.Writer
Go
126
star
3

awslim

A simplified alternative to the AWS CLI for limited use cases.
Go
114
star
4

fluent-agent-hydra

A Fluentd log agent.
Go
108
star
5

tracer

ECS task event/log tracer CLI
Go
95
star
6

tfstate-lookup

Lookup resource attributes in tfstate.
Go
87
star
7

ecsta

ECS Task Assistant tool.
Go
59
star
8

ridge

AWS Lambda HTTP Proxy integration event bridge to Go net/http.
Go
55
star
9

ecrm

A command line tool for managing ECR repositories.
Go
54
star
10

consul-kv-dashboard

Consul KVS based dashboard web application.
Go
49
star
11

fluent-plugin-zabbix

fluentd out plugin to zabbix
Ruby
49
star
12

kinesis-tailf

tail -f command for Amazon Kinesis Stream
Go
38
star
13

go-zabbix-get

zabbix-get compatible command (Golang version)
Go
28
star
14

Rin

Rin is a Redshift data Importer by SQS messaging.
Go
26
star
15

chef-solo-with-capistrano

chef-solo by capistrano
Ruby
26
star
16

cfft

cfft is a testing tool for CloudFront Functions.
Go
26
star
17

nssh

Go
22
star
18

aswrap

AWS assume role credential wrapper
Perl
21
star
19

tfstate-merge

A tool to merge tfstate files.
Ruby
20
star
20

mysql-slave-healthcheck-agent

Go
17
star
21

tuggle

Distributed file mirroring proxy in Consul cluster
Go
16
star
22

zabbix-aggregate-agent

Data aggregator for multiple zabbix-agent
Go
15
star
23

fluent-plugin-suppress

fluentd plugin to suppress same messages.
Ruby
14
star
24

isucon11-f

Vue
13
star
25

raus

Raus: Ranged unique id supplier
Go
12
star
26

MHA-AWS

A support script for MySQL MasterHA which running on Amazon Web Service.
Perl
12
star
27

lamblocal

Go
12
star
28

go-redis-setlock

Like the setlock command using Redis.
Perl
11
star
29

sailtrim

A minimal deployment tool for Amazon Lightsail.
Go
11
star
30

ohai-plugin-consul

Ohai plugin for Consul API
Ruby
10
star
31

maprobe

Mackerel external probe agent
Go
9
star
32

perl-queue-q4pg-lite

simple message queue using PostgreSQL.
Perl
8
star
33

p5-Parallel-Benchmark

Perl
8
star
34

Urume

Perl
7
star
35

AWS-XRay

AWS X-Ray tracing library for Perl5
Perl
6
star
36

isucon11-q

Go
6
star
37

knockrd

HTTP knocker daemon
Go
6
star
38

riex

AWS RI expiration detector
Go
5
star
39

go-amzn-oidc

Validator for x-amzn-oidc-data header values.
Go
5
star
40

sardine

Mackerel plugin metrics aggregator with CloudWatch.
Go
4
star
41

maws-cli

A multiplexing wrapper for aws cli.
Go
4
star
42

isucon5q

C
4
star
43

mackerel-plugin-prometheus-query

Prometheus query plugin for Mackerel
Go
4
star
44

go-simple-kvs

simple and tiny key value store
Go
4
star
45

consul-lock

runs another program with a Consul session/kv locked.
Go
4
star
46

fluent-plugin-imkayac

Fluentd plugin to post im.kayac.com
Ruby
4
star
47

iam-policy-finder

iam-policy-finder is finder of AWS IAM Policies.
Go
4
star
48

rrdialer

round robin net dialer.
Go
3
star
49

sirbot

Slack IRC relay bot
Go
3
star
50

Redis-Setlock

Like the setlock command using Redis.
Perl
3
star
51

fluent-plugin-firelens-tag-filter

fluent plugin for AWS firelens
Ruby
3
star
52

Acore

Perl
3
star
53

s32cs

Amazon CloudSearch document uploader via S3 event notification.
Go
3
star
54

go-http-transport-firehose

Go HTTP Transport to dump a request to Firehose
Go
3
star
55

isucon7q

HTML
3
star
56

cfn-lookup

Lookup outputs and exports for CloudFormation stacks.
Go
3
star
57

cloudwatch-to-mackerel

Copy metrics from Amazon CloudWatch to Mackerel.
Go
2
star
58

go-jsqlite

SQL query runner for JSONL
Go
2
star
59

lamux

Lamux is a HTTP multiplexer for AWS Lambda Function aliases.
Go
2
star
60

ssm-lookup

Lookup values in AWS SSM Parameter store.
Go
2
star
61

norikra-listener-mackerel

Norikra listener plugin to post service metrics for Mackerel.
Ruby
2
star
62

Devel-KYTProf-Profiler-Redis-Fast

KYTProf profiler for Redis::Fast
Perl
1
star
63

isucon5

Perl
1
star
64

Plack-Middleware-XRay

Plack middleware for AWS X-Ray tracing
Perl
1
star
65

redisqs

redisqs is a delayed queue manager using Amazon SQS and Redis.
Go
1
star
66

Plack-Middleware-GTop-ProcMem

Plack middleware for mesuring process memory.
Perl
1
star
67

macaroni

macaroni is a reporter for Songmu/horenso.
Go
1
star
68

Plack-Middleware-SetLocalEnv

Set localized environment variables(Perl's %ENV) from the value of PSGI environment.
Perl
1
star
69

ktlog2memcached

KyotoTycoon's slave agent, replicate updates to memcached.
Perl
1
star
70

mkr2oncall-cloudflare-workers

A port of mackerel-to-grafana-oncall for Cloudflare Workers.
TypeScript
1
star
71

Test-UNIXSock

testing UNIX domain socket program
Perl
1
star
72

go-template

Go
1
star
73

distdin

Distribute stdin to multiple sub commands.
Go
1
star
74

mirage-ecs-example

Dockerfile
1
star
75

go-s3concat

Concat S3 objects
Go
1
star
76

lambroll_test

Makefile
1
star
77

p5-dbix-couchlike

Perl
1
star