-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cbbdc1
commit 3e86ee2
Showing
4 changed files
with
148 additions
and
27 deletions.
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
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 @@ | ||
exports.handler = function (_, context) { | ||
return context.succeed({ | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: "Hello World", | ||
}), | ||
}); | ||
}; |
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,19 @@ | ||
exports.handler = function (_, context) { | ||
const rand = Math.random(); | ||
|
||
if (rand < 0.5) { | ||
return context.succeed({ | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: "Hello World", | ||
}), | ||
}); | ||
} else { | ||
return context.fail({ | ||
statusCode: 500, | ||
body: JSON.stringify({ | ||
error: "Something went wrong", | ||
}), | ||
}); | ||
} | ||
}; |
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,99 @@ | ||
# MultiTool Canary Quickstart Guide | ||
|
||
In this guide, you'll learn how to safely deploy an AWS Lambda function as an API endpoint using the AWS API Gateway. | ||
|
||
# Prerequisites | ||
|
||
1. You must have an AWS account with root user access or an IAM user with read and write permissions for Lambda and API Gateway | ||
1. Install the AWS CLI. [Click here for instructions.](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) | ||
|
||
1. Either login with `aws sso login --profile my-profile` or set your Access Key Id and Secret Access Key: | ||
|
||
```bash | ||
$ aws sts get-caller-identity | ||
$ export AWS_ACCESS_KEY_ID="${MY_ACCESS_KEY}" | ||
$ export AWS_SECRET_ACCESS_KEY="${MY_SECRET_ACCESS_KEY}" | ||
$ export AWS_REGION="us-east-2" | ||
``` | ||
|
||
1. Install MultiTool Canary. [Click here for instructions.](https://github.com/wack/canary?tab=readme-ov-file#installation) | ||
|
||
# Getting Started with AWS Lambda and API Gateway | ||
|
||
## Package your Lambda as a zip file | ||
|
||
```bash | ||
$ zip -j base.zip guides/quickstart/lambda_code/base/index.js | ||
``` | ||
|
||
## Create a Lambda execution IAM role | ||
|
||
```bash | ||
$ LAMBDA_EXECUTION_ROLE_ARN=$(aws iam create-role \ | ||
--role-name lambda-execution \ | ||
--assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' --output text --query Role.Arn) | ||
``` | ||
|
||
## Create a Lambda using the zip file | ||
|
||
```bash | ||
$ LAMBDA_ARN=$(aws lambda create-function --function-name canary-quickstart-lambda \ | ||
--runtime nodejs22.x \ | ||
--handler index.handler \ | ||
--role ${LAMBDA_EXECUTION_ROLE_ARN} \ | ||
--zip-file fileb://base.zip \ | ||
--output text \ | ||
--query FunctionArn) | ||
``` | ||
|
||
## Create an API Gateway | ||
|
||
```bash | ||
$ API_ID=$(aws apigateway create-rest-api --name canary-quickstart-apig --output text --query id ) | ||
``` | ||
|
||
## Get the Root resource's auto-generated ID | ||
|
||
```bash | ||
$ ROOT_RESOURCE_ID=$(aws apigateway get-resources --rest-api-id ${API_ID} --output text --query items[0].id) | ||
``` | ||
|
||
## Create a resource in the gateway | ||
|
||
```bash | ||
$ RESOURCE_ID=$(aws apigateway create-resource --rest-api-id ${API_ID} --parent-id ${ROOT_RESOURCE_ID} --path-part "demo" --output text --query 'id') | ||
``` | ||
|
||
## Add a GET endpoint method to the resource | ||
|
||
```bash | ||
$ aws apigateway put-method --rest-api-id ${API_ID} --resource-id ${RESOURCE_ID} --http-method GET --authorization-type "NONE" | ||
``` | ||
|
||
## Update the API Gateway to point at the lambda we created | ||
|
||
```bash | ||
$ aws apigateway put-integration \ | ||
--rest-api-id ${API_ID} \ | ||
--resource-id ${RESOURCE_ID} \ | ||
--http-method GET \ | ||
--type AWS_PROXY \ | ||
--integration-http-method POST \ | ||
--uri arn:aws:apigateway:${AWS_REGION:=us-east-2}:lambda:path/2015-03-31/functions/${LAMBDA_ARN}/invocations | ||
``` | ||
|
||
## Create a deployment | ||
|
||
```bash | ||
$ $ aws apigateway create-deployment --rest-api-id $API_ID --stage-name prod | ||
``` | ||
|
||
## Give permissions for the API Gateway to invoke the Lambda | ||
|
||
```bash | ||
$ aws lambda add-permission \ | ||
--function-name canary-quickstart-lambda \ | ||
--statement-id apigateway-permission-${API_ID} \ | ||
--action lambda:InvokeFunction \ | ||
--principal apigateway.amazonaws.com | ||
``` |