samcli/lib/init/templates/cookiecutter-aws-sam-hello-golang/{{cookiecutter.project_name}}/README.md
This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you:
.
├── README.md <-- This instructions file
├── hello-world <-- Source code for a lambda function
│ ├── go.mod <-- Go dependency definitions
│ ├── main.go <-- Lambda function code
│ └── main_test.go <-- Unit tests
└── template.yaml
Golang is a statically compiled language, meaning that in order to run it you have to build the executable target. Ensure your go.mod file in your application directory has all of your dependencies, and simply run sam build to build your application using Go modules.
Invoking function locally through local API Gateway
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:
...
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
AWS Lambda Python runtime requires a flat folder with all dependencies including the application. SAM will use CodeUri property to know where to look up for both application and dependencies:
...
FirstFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
...
To deploy your application for the first time, run the following in your shell:
sam deploy --guided
The command will package and deploy your application to AWS, with a series of prompts:
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.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.
We use testing package that is built-in in Golang and you can simply run the following command to run our tests:
go test -v ./hello-world/
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.
Issue the following command from the terminal:
brew install golang
If it's already installed, run the following command to ensure it's the latest version:
brew update
brew upgrade golang
Issue the following command from the powershell:
choco install golang
If it's already installed, run the following command to ensure it's the latest version:
choco upgrade golang
Here are a few ideas that you can use to get more acquainted as to how this overall process works:
Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: