LoginSignup
0
0

AWS lambda のcoding/upload/invokeをローカルで完結させる

Last updated at Posted at 2023-06-07

Put this Makefile in your local machine.

.PHONY: all
all: zip upload invoke

.PHONY: zip
zip:
	# TODO: Don't update this Makefile (^^)
	# TODO: I would like to use `zip -FSr function.zip .` to speed up, but sometimes it did not work.
    # NOTE: To exclude specific files, use an option `--exclude "node_modules/*"`
	zip -r - . --exclude function.zip > function.zip

.PHONY: upload
upload:
	aws lambda update-function-code --function-name ${FUNCTION_NAME} --zip-file fileb://function.zip --region ${AWS_REGION} --profile ${AWS_ROLE}  | jq .

.PHONY: invoke
invoke:
	aws lambda invoke response.json --function-name ${FUNCTION_NAME} --invocation-type RequestResponse --log-type Tail --region ${AWS_REGION} --profile ${AWS_ROLE} --payload file://payload.json --cli-binary-format raw-in-base64-out > log.json\
	&& echo "\n################### Log #########################\n"\
	&& cat log.json | jq --raw-output '.LogResult' | base64 --decode\
	&& echo "\n################### Response #####################\n"\
	&& cat log.json | jq '.StatusCode'\
	&& cat response.json | jq .\
	&& rm log.json\
	&& rm response.json

Also, put the content which you would like to pass as event arg in payload.json.

{
    "version": "0",
    "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
    "detail-type": "Scheduled Event",
    "source": "aws.events",
    "account": "123456789012",
    "time": "2015-10-08T16:53:06Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
    ],
    "detail": {}
}

Substitute enviromental variables, and run make all.

export AWS_ROLE=your_aws_role
export AWS_REGION=us-east-1
export FUNCTION_NAME=your-lambda-function-name

make all
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0