2020-05-27 13:29:39 -07:00
# AWS SDK for Go code examples for AWS Lambda
## Purpose
2020-06-15 10:23:10 -07:00
These examples demonstrate how to perform several AWS Lambda operations.
2020-05-27 13:29:39 -07:00
## Prerequisites
You must have an AWS account, and have your default credentials and AWS Region
configured as described in
[Configuring the AWS SDK for Go ](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html )
in the _ AWS SDK for Go Developer Guide _ .
## Running the code
2020-05-28 13:29:04 -07:00
### ConfigureFunction/ConfigureFunction.go
2020-06-15 10:23:10 -07:00
This example enables an Amazon S3 bucket to send notifications to an AWS Lambda function.
2020-05-28 13:29:04 -07:00
`go run ConfigureFunction.go -f FUNCTION -a ARN`
2020-06-15 10:23:10 -07:00
- _FUNCTION_ is the name of the AWS Lambda function.
- _ARN_ is the Amazon Resource Name (ARN) of the S3 bucket sending a notification to the function.
2020-05-28 13:29:04 -07:00
The unit test mocks the service client and the `AddPermission` function.
2020-05-27 13:29:39 -07:00
### MakeFunction/MakeFunction.go
2020-06-15 10:23:10 -07:00
This example creates an AWS Lambda function.
2020-05-27 13:29:39 -07:00
`go run MakeFunction.go -z ZIPFILE -b BUCKET -f FUNCTION -h HANDLER -a ROLE-ARN -r RUNTIME`
2020-06-15 10:23:10 -07:00
- _ZIPFILE_ is the name of the zip file, without the .zip extension, containing the AWS Lambda function code.
- _BUCKET_ is the name of an Amazon S3 bucket in the same AWS Region as the AWS Lambda function.
- _FUNCTION_ is the name of the AWS Lambda function.
- _HANDLER_ is the name of the method within your code that AWS Lambda calls.
2020-05-27 13:29:39 -07:00
- _ROLE-ARN_ is the ARN of the function's execution role.
- _RUNTIME_ is the identifier of the function's runtime.
The unit test mocks the service client and the `CreateFunction` function.
2020-05-28 13:29:04 -07:00
### RunFunction/RunFunction.go
2020-06-15 10:23:10 -07:00
This example executes an AWS Lambda function.
2020-05-28 13:29:04 -07:00
`go run RunFunction.go -f FUNCTION -f FUNCTION [-m MAX-ITEMS]`
2020-06-15 10:23:10 -07:00
- _FUNCTION_ is the name of the AWS Lambda function to call.
2020-05-28 13:29:04 -07:00
- _MAX-ITEMS_ is the maximum number of items to retrieve.
This argument is optional, and if not supplied, defaults to 10.
The unit test mocks the service client and the `Invoke` function.
2020-05-27 13:29:39 -07:00
### ShowFunctions/ShowFunctions.go
2020-06-15 10:23:10 -07:00
This example displays a list of your AWS Lambda functions.
2020-05-27 13:29:39 -07:00
`go run ShowFunctions.go`
The unit test mocks the service client and the `ListFunctions` function.
### Notes
- We recommend that you grant this code least privilege,
or at most the minimum permissions required to perform the task.
For more information, see
[Grant Least Privilege ](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege )
in the _ AWS Identity and Access Management User Guide _ .
- This code has not been tested in all AWS Regions.
Some AWS services are available only in specific
[Regions ](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services ).
- Running this code might result in charges to your AWS account.
## Running the unit tests
Unit tests should delete any resources they create.
However, they might result in charges to your
AWS account.
To run a unit test, enter the following.
`go test`
You should see something like the following,
where PATH is the path to the folder containing the Go files.
``` sh
PASS
ok PATH 6.593s
```
To see any log messages, enter the following.
`go test -test.v`
You should see additional log messages.
The last two lines should be similar to the previous output shown.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0