-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Provided.al2023 implementation (#139) * Provided.al2023 implementation * Add events file to Go hello-img project. * Fix manifest-v2 for providedal2023 runtimes. * Add test build invoke for go AL2023. * Add test build invoke for Rust AL2023. * Add Go AL2023 unit test. * Add Rust AL2023 unit test. --------- Co-authored-by: Sean O Brien <[email protected]> * update manifest * fix cookiecutter.json --------- Co-authored-by: Anton Stepanov <[email protected]> Co-authored-by: Sean O Brien <[email protected]>
- Loading branch information
1 parent
e446d43
commit f6977dd
Showing
41 changed files
with
1,616 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Cookiecutter SAM for golang Lambda functions | ||
|
||
This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless App based on Serverless Application Model (SAM). | ||
|
||
It is important to note that you should not try to `git clone` this project but use `cookiecutter` CLI instead as ``\\{\\{cookiecutter.project_slug\\}\\}`` will be rendered based on your input and therefore all variables and files will be rendered properly. | ||
|
||
## Requirements | ||
|
||
Install `cookiecutter` command line: | ||
|
||
**Pip users**: | ||
|
||
* `pip install cookiecutter` | ||
|
||
**Homebrew users**: | ||
|
||
* `brew install cookiecutter` | ||
|
||
**Windows or Pipenv users**: | ||
|
||
* `pipenv install cookiecutter` | ||
|
||
**NOTE**: [`Pipenv`](https://github.com/pypa/pipenv) is the new and recommended Python packaging tool that works across multiple platforms and makes Windows a first-class citizen. | ||
|
||
## Usage | ||
|
||
Generate a new SAM based Serverless App: `cookiecutter gh:aws-samples/cookiecutter-aws-sam-golang`. | ||
|
||
You'll be prompted a few questions to help this cookiecutter template to scaffold this project and after its completed you should see a new folder at your current path with the name of the project you gave as input. | ||
|
||
**NOTE**: After you understand how cookiecutter works (cookiecutter.json, mainly), you can fork this repo and apply your own mechanisms to accelerate your development process and this can be followed for any programming language and OS. | ||
|
||
## Options | ||
|
||
Option | Description | ||
------------------------------------------------- | --------------------------------------------------------------------------------- | ||
`include_apigw` | Includes sample code for API Gateway Proxy integration for Lambda and a Catch All method in SAM as a starting point | ||
`include_xray` | Includes both sample code for getting started with AWS X-Ray and adds necessary permission and `Tracing` to your function | ||
`include_safe_deployment` | Sends by default 10% of traffic for every 1 minute to a newly deployed function using [CodeDeploy + SAM integration](https://github.com/awslabs/serverless-application-model/blob/master/docs/safe_lambda_deployments.rst) - Linear10PercentEvery1Minute | ||
`include_experimental_make` | Includes a `Makefile` for advanced users to automate packaging, build, tests and SAM Local - Only works on OSX/Linux at the moment | ||
|
||
## Credits | ||
|
||
* This project has been generated with [Cookiecutter](https://github.com/audreyr/cookiecutter) | ||
* [Bruno Alla's Lambda function template](https://github.com/browniebroke/cookiecutter-lambda-function) | ||
|
||
## License | ||
|
||
This project is licensed under the terms of the [MIT License with no attribution](/LICENSE) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"project_name": "Name of the project", | ||
"runtime": "go", | ||
"architectures": { | ||
"value": [ "x86_64", "arm64" ] | ||
}, | ||
"_copy_without_render": [ | ||
".gitignore" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
cookiecutter==2.1.1 | ||
flake8==3.5.0 | ||
pytest==3.3.2 | ||
pytest-cookies==0.3.0 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from contextlib import contextmanager | ||
|
||
import os | ||
|
||
|
||
@contextmanager | ||
def inside_dir(dirpath): | ||
""" | ||
Execute code from inside the given directory | ||
:param dirpath: String, path of the directory the command is being run. | ||
""" | ||
old_path = os.getcwd() | ||
try: | ||
os.chdir(dirpath) | ||
yield | ||
finally: | ||
os.chdir(old_path) | ||
|
||
|
||
def test_project_tree(cookies): | ||
result = cookies.bake(extra_context={"project_name": "test_project"}) | ||
assert result.exit_code == 0 | ||
assert result.exception is None | ||
assert result.project.basename == "test_project" | ||
|
||
assert result.project.isdir() | ||
assert result.project.join("README.md").isfile() | ||
assert result.project.join("template.yaml").isfile() | ||
assert result.project.join("hello-world").isdir() | ||
assert result.project.join("hello-world", "main.go").isfile() | ||
assert result.project.join("hello-world", "main_test.go").isfile() | ||
|
||
|
||
def test_app_content(cookies): | ||
result = cookies.bake(extra_context={"project_name": "test_project"}) | ||
app_file = result.project.join("hello-world", "main.go") | ||
app_content = app_file.readlines() | ||
app_content = "".join(app_content) | ||
|
||
contents = ( | ||
"github.com/aws/aws-lambda-go/events", | ||
"sourceIP := request.RequestContext.Identity.SourceIP", | ||
"lambda.Start(handler)" | ||
) | ||
|
||
for content in contents: | ||
assert content in app_content | ||
|
||
|
||
def test_app_test_content(cookies): | ||
result = cookies.bake(extra_context={"project_name": "test_project"}) | ||
app_file = result.project.join("hello-world", "main_test.go") | ||
app_content = app_file.readlines() | ||
app_content = "".join(app_content) | ||
|
||
contents = ( | ||
"func TestHandler(t *testing.T)", | ||
"SourceIP: \"127.0.0.1\"", | ||
"response, err := handler(testCase.request)" | ||
) | ||
|
||
for content in contents: | ||
assert content in app_content | ||
|
||
|
||
def test_app_template_content(cookies): | ||
result = cookies.bake(extra_context={"project_name": "test_project"}) | ||
app_file = result.project.join("template.yaml") | ||
app_content = app_file.readlines() | ||
app_content = "".join(app_content) | ||
|
||
contents = ( | ||
"Runtime: go1.x", | ||
"HelloWorldFunction", | ||
) | ||
|
||
for content in contents: | ||
assert content in app_content |
137 changes: 137 additions & 0 deletions
137
al2023/go/hello-img/{{cookiecutter.project_name}}/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# {{ cookiecutter.project_name }} | ||
|
||
This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: | ||
|
||
```bash | ||
. | ||
├── README.md <-- This instructions file | ||
├── hello-world <-- Source code for a lambda function | ||
│ ├── main.go <-- Lambda function code | ||
│ └── main_test.go <-- Unit tests | ||
│ └── Dockerfile <-- Dockerfile | ||
└── template.yaml | ||
``` | ||
|
||
## Requirements | ||
|
||
* AWS CLI already configured with Administrator permission | ||
* [Docker installed](https://www.docker.com/community-edition) | ||
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) | ||
|
||
You may need the following for local testing. | ||
* [Golang](https://golang.org) | ||
|
||
## Setup process | ||
|
||
### Installing dependencies & building the target | ||
|
||
In this example we use the built-in `sam build` to build a docker image from a Dockerfile and then copy the source of your application inside the Docker image. | ||
Read more about [SAM Build here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html) | ||
|
||
### Local development | ||
|
||
**Invoking function locally through local API Gateway** | ||
|
||
```bash | ||
sam local start-api | ||
``` | ||
|
||
If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` | ||
|
||
**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: | ||
|
||
```yaml | ||
... | ||
Events: | ||
HelloWorld: | ||
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api | ||
Properties: | ||
Path: /hello | ||
Method: get | ||
``` | ||
## Packaging and deployment | ||
AWS Lambda Golang runtime requires a flat folder with the executable generated on build step. SAM will use `CodeUri` property to know where to look up for the application: | ||
|
||
```yaml | ||
... | ||
FirstFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: hello_world/ | ||
... | ||
``` | ||
|
||
To deploy your application for the first time, run the following in your shell: | ||
|
||
```bash | ||
sam deploy --guided | ||
``` | ||
|
||
The command will package and deploy your application to AWS, with a series of prompts: | ||
|
||
* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. | ||
* **AWS Region**: The AWS region you want to deploy your app to. | ||
* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. | ||
* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. | ||
* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. | ||
|
||
You can find your API Gateway Endpoint URL in the output values displayed after deployment. | ||
|
||
### Testing | ||
|
||
We use `testing` package that is built-in in Golang and you can simply run the following command to run our tests locally: | ||
|
||
```shell | ||
cd ./hello-world/ | ||
go test -v . | ||
``` | ||
# Appendix | ||
|
||
### Golang installation | ||
|
||
Please ensure Go 1.x (where 'x' is the latest version) is installed as per the instructions on the official golang website: https://golang.org/doc/install | ||
|
||
A quickstart way would be to use Homebrew, chocolatey or your linux package manager. | ||
|
||
#### Homebrew (Mac) | ||
|
||
Issue the following command from the terminal: | ||
|
||
```shell | ||
brew install golang | ||
``` | ||
|
||
If it's already installed, run the following command to ensure it's the latest version: | ||
|
||
```shell | ||
brew update | ||
brew upgrade golang | ||
``` | ||
|
||
#### Chocolatey (Windows) | ||
|
||
Issue the following command from the powershell: | ||
|
||
```shell | ||
choco install golang | ||
``` | ||
|
||
If it's already installed, run the following command to ensure it's the latest version: | ||
|
||
```shell | ||
choco upgrade golang | ||
``` | ||
|
||
## Bringing to the next level | ||
|
||
Here are a few ideas that you can use to get more acquainted as to how this overall process works: | ||
|
||
* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path | ||
* Update unit test to capture that | ||
* Package & Deploy | ||
|
||
Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: | ||
|
||
* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/) |
63 changes: 63 additions & 0 deletions
63
al2023/go/hello-img/{{cookiecutter.project_name}}/events/event.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{ | ||
"body": "{\"message\": \"hello world\"}", | ||
"resource": "/hello", | ||
"path": "/hello", | ||
"httpMethod": "GET", | ||
"isBase64Encoded": false, | ||
"queryStringParameters": { | ||
"foo": "bar" | ||
}, | ||
"pathParameters": { | ||
"proxy": "/path/to/resource" | ||
}, | ||
"stageVariables": { | ||
"baz": "qux" | ||
}, | ||
"headers": { | ||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||
"Accept-Encoding": "gzip, deflate, sdch", | ||
"Accept-Language": "en-US,en;q=0.8", | ||
"Cache-Control": "max-age=0", | ||
"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": "US", | ||
"Host": "1234567890.execute-api.us-east-1.amazonaws.com", | ||
"Upgrade-Insecure-Requests": "1", | ||
"User-Agent": "Custom User Agent String", | ||
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", | ||
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", | ||
"X-Forwarded-For": "127.0.0.1, 127.0.0.2", | ||
"X-Forwarded-Port": "443", | ||
"X-Forwarded-Proto": "https" | ||
}, | ||
"requestContext": { | ||
"accountId": "123456789012", | ||
"resourceId": "123456", | ||
"stage": "prod", | ||
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", | ||
"requestTime": "09/Apr/2015:12:34:56 +0000", | ||
"requestTimeEpoch": 1428582896000, | ||
"identity": { | ||
"cognitoIdentityPoolId": null, | ||
"accountId": null, | ||
"cognitoIdentityId": null, | ||
"caller": null, | ||
"accessKey": null, | ||
"sourceIp": "127.0.0.1", | ||
"cognitoAuthenticationType": null, | ||
"cognitoAuthenticationProvider": null, | ||
"userArn": null, | ||
"userAgent": "Custom User Agent String", | ||
"user": null | ||
}, | ||
"path": "/prod/hello", | ||
"resourcePath": "/hello", | ||
"httpMethod": "POST", | ||
"apiId": "1234567890", | ||
"protocol": "HTTP/1.1" | ||
} | ||
} | ||
|
7 changes: 7 additions & 0 deletions
7
al2023/go/hello-img/{{cookiecutter.project_name}}/hello-world/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM public.ecr.aws/docker/library/golang:1.19 as build-image | ||
WORKDIR /src | ||
COPY go.mod go.sum main.go ./ | ||
RUN go build -o lambda-handler | ||
FROM public.ecr.aws/lambda/provided:al2023 | ||
COPY --from=build-image /src/lambda-handler . | ||
ENTRYPOINT ./lambda-handler |
5 changes: 5 additions & 0 deletions
5
al2023/go/hello-img/{{cookiecutter.project_name}}/hello-world/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module hello-world | ||
|
||
go 1.19 | ||
|
||
require github.com/aws/aws-lambda-go v1.36.1 |
8 changes: 8 additions & 0 deletions
8
al2023/go/hello-img/{{cookiecutter.project_name}}/hello-world/go.sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/aws/aws-lambda-go v1.35.0 h1:iocVDy5Cw5SCRrKOPHwarkdFwwy48OkfmHoE6SJ3ATg= | ||
github.com/aws/aws-lambda-go v1.35.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM= | ||
github.com/aws/aws-lambda-go v1.36.1 h1:CJxGkL9uKszIASRDxzcOcLX6juzTLoTKtCIgUGcTjTU= | ||
github.com/aws/aws-lambda-go v1.36.1/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
Oops, something went wrong.