-
Notifications
You must be signed in to change notification settings - Fork 660
chore: Add guide for running lambda locally #1506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d48d6bb
Add guide for running lambda locally
npalm 3f1f033
review
npalm 5ad5c60
review
npalm 5bbbac0
review
npalm 854ff9b
review
npalm 6673bd3
review
npalm 8f6e790
review
npalm 21819c9
review
npalm f557796
review
npalm f5d1fea
review
npalm b8d0b79
review
npalm ae386b7
review
npalm 3b3a4cf
review
npalm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,88 @@ | ||
# Lambda - Test locally | ||
|
||
This README provide guidance for testing the lambda locally / and or in AWS. This guide assumes you are familiar with AWS, lambda and Node. If not mention explicit comments provided should be executed from the root of the lambda package. | ||
|
||
## Testing in AWS | ||
|
||
Just navigate to AWS Lambda en trigger a test event. Provide an event that match the required input. For lambdas that does not require a specific event, just sent any event. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
## Testing locally | ||
|
||
Testing locally can be done in two ways. Using aws SAM framework, or run via a wrapper to simulate the event to invoke the lambda. Both setups requires that the required input environment variables are set, and AWS resources on which the lambda depends are available. We advise for testing the lambda locally to first create your own deployment to AWS, this will simplify the setup on depended AWS resources. For example based on the de [default example](../../../../examples/default/). | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Locally test setups instruction are available for the following lambda's | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- [runner-binary-syncer](./moduele/../../modules/runner-binaries-syncer/lambdas/runner-binaries-syncer) - This lambda does not need any input, no event is required. Supported via SAM and local Node. | ||
|
||
### Extend deployment configuration | ||
|
||
Add the code below to your Terraform deployment, to allow your principal to use the Lambda role, and retrieve the lambda configuration. Update your Terraform deployment and apply the changes. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```hcl | ||
data "aws_caller_identity" "current" {} | ||
|
||
module "runners" { | ||
|
||
... | ||
|
||
# Assume you have a profile with Admin privileges, allow yo to switch to the Lambda role | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lambda_principals = [{ | ||
type = "AWS" | ||
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] | ||
}] | ||
|
||
} | ||
|
||
output "development" { | ||
value = { | ||
lambda_syncer = module.runners.binaries_syncer.lambda | ||
} | ||
}``` | ||
|
||
Once you have updated your Terraform deployment read the lambda configuration into your environment. Run the commands below in your Terraform workspace folder. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
LAMBDA_ENV=$(terraform output -json development | jq -r '.lambda_syncer.environment[].variables' | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]") | ||
for x in $LAMBDA_ENV ; do echo setting $x; export $x; done | ||
``` | ||
|
||
### Testing with SAM | ||
|
||
This setup requires AWS SAM cli and Docker is installed. First update the AWS config (`~/.aws/config`) so you can use easy switch to the role used by the lambda. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```properties | ||
[profile gh-development] | ||
source_profile=<OPTIONAL_SOURCE_PROFILE> | ||
region=<DEFAULT_REGION> | ||
role_arn=<ARN_CHECK_TF_OUTPUT> | ||
``` | ||
|
||
Now you can set the profile and region as environment variable or pass as argument to sam. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` | ||
export AWS_REGION=<region> | ||
export AWS_PROFILE=gh-development | ||
``` | ||
|
||
For SAM a `template.yml` defines the lambda for running local. Thats all, now build your the lambda with `yarn run dist` and next invoke the lambda with `sam local invoke`. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
### With Node | ||
|
||
Alternatively of using SAM you can use Node with ts-node-dev to test the code locally. The drawback is that you have to setup AWS credentials into you shell. Also you are depending on a tiny wrapper (`local.ts`), and your local Node version. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The AWS SDK seems not to handle environment variables for profile, the only option to pass the role is via credentials. You can get credentials via STS for the role. | ||
npalm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
|
||
role=$(aws sts assume-role \ | ||
--role-arn "<ROLE>" \ | ||
--duration-seconds 3600 --role-session-name "dev") | ||
|
||
export AWS_ACCESS_KEY_ID=$(echo $role | jq -r .Credentials.AccessKeyId) | ||
export AWS_SECRET_ACCESS_KEY=$(echo $role | jq -r .Credentials.SecretAccessKey) | ||
export AWS_SESSION_TOKEN=$(echo $role | jq -r .Credentials.SessionToken) | ||
``` | ||
|
||
Next set the region `export AWS_REGION=<region>`. Now you can run the lambda locally via `yarn run watch`. |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.