Skip to content

Instantly share code, notes, and snippets.

@galiminus
Last active February 18, 2020 08:59
Show Gist options
  • Save galiminus/89b136caaf05dcdbee294c38b6f42d5a to your computer and use it in GitHub Desktop.
Save galiminus/89b136caaf05dcdbee294c38b6f42d5a to your computer and use it in GitHub Desktop.

Install the serverless framework

https://serverless.com/

npm install -g serverless

Assuming you created a project named lambda-training, two files will be created in the lambda-training/ directory. The serverless.yml file is a configuration file that is heavily documented. The other file is handler.py which contains your lambda handler.

It is possible to have multiple lambda function (and therefore multiple handlers) in the same project, sharing code happily.

Create a new project

cd parent-directory
sls # and answer the few questions

Update the serverless.yml file with those values:

provider:
  name: aws
  runtime: python3.8
  stage: dev
  region: eu-west-1

Deployment

sls deploy

Integrate with API Gateway

Update the serverless.yml file to add an API Gateway trigger:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: test/path
          method: get

After another sls deploy the log will contain the full URL of the new trigger. You can just cURL to see if everything is working as expected:

curl https://m0tiewu09g.execute-api.eu-west-1.amazonaws.com/dev/test/path
{"message": "Go Serverless v1.0! Your function executed successfully!", "input": {"resource": "/test/path", "path": "/test/path", "httpMethod": "GET", "headers": {"Accept": "*/*", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "FR", "Host": "m0tiewu09g.execute-api.eu-west-1.amazonaws.com", "User-Agent": "curl/7.64.1", "Via": "2.0 47140f009c2bd3561cd6dde4003253e3.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "1K_equfBEMmbcNzevk63twWezyRpZTH5PUS39VtZz4XP3flmyxDQog==", "X-Amzn-Trace-Id": "Root=1-5e4aa8d1-e832e8eed84e4c9843ad08b4", "X-Forwarded-For": "90.109.35.122, 70.132.16.75", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https"}, "multiValueHeaders": {"Accept": ["*/*"], "CloudFront-Forwarded-Proto": ["https"], "CloudFront-Is-Desktop-Viewer": ["true"], "CloudFront-Is-Mobile-Viewer": ["false"], "CloudFront-Is-SmartTV-Viewer": ["false"], "CloudFront-Is-Tablet-Viewer": ["false"], "CloudFront-Viewer-Country": ["FR"], "Host": ["m0tiewu09g.execute-api.eu-west-1.amazonaws.com"], "User-Agent": ["curl/7.64.1"], "Via": ["2.0 47140f009c2bd3561cd6dde4003253e3.cloudfront.net (CloudFront)"], "X-Amz-Cf-Id": ["1K_equfBEMmbcNzevk63twWezyRpZTH5PUS39VtZz4XP3flmyxDQog=="], "X-Amzn-Trace-Id": ["Root=1-5e4aa8d1-e832e8eed84e4c9843ad08b4"], "X-Forwarded-For": ["90.109.35.122, 70.132.16.75"], "X-Forwarded-Port": ["443"], "X-Forwarded-Proto": ["https"]}, "queryStringParameters": null, "multiValueQueryStringParameters": null, "pathParameters": null, "stageVariables": null, "requestContext": {"resourceId": "rj6d3x", "resourcePath": "/test/path", "httpMethod": "GET", "extendedRequestId": "IC9Q0FKijoEFRaw=", "requestTime": "17/Feb/2020:14:53:05 +0000", "path": "/dev/test/path", "accountId": "651828462322", "protocol": "HTTP/1.1", "stage": "dev", "domainPrefix": "m0tiewu09g", "requestTimeEpoch": 1581951185934, "requestId": "199a0bfe-1475-4537-b92b-dfbbba4d82a1", "identity": {"cognitoIdentityPoolId": null, "accountId": null, "cognitoIdentityId": null, "caller": null, "sourceIp": "90.109.35.122", "principalOrgId": null, "accessKey": null, "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": null, "userAgent": "curl/7.64.1", "user": null}, "domainName": "m0tiewu09g.execute-api.eu-west-1.amazonaws.com", "apiId": "m0tiewu09g"}, "body": null, "isBase64Encoded": false}}% 

Test it locally

Create an event.json file with your event sample

{
  "queryStringParameters": {
    "first_param": "any value",
    "second_param": "any other value"
  }
}
serverless invoke local -f hello -p event.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment