From 47ed7e72dbeb6d1ee500c780f804165afe7177d2 Mon Sep 17 00:00:00 2001 From: CHIKAMATSU Naohiro Date: Fri, 9 Feb 2024 22:44:55 +0900 Subject: [PATCH] Lambda with API Gateway --- .../api-gateway-with-lambda/.gitignore | 1 + .../api-gateway-with-lambda/Makefile | 28 ++++++++ .../api-gateway-with-lambda/README.md | 0 .../api-gateway-with-lambda/main.go | 37 ++++++++++ .../api-gateway-with-lambda/template.yml | 71 +++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 cloudformation/api-gateway-with-lambda/.gitignore create mode 100644 cloudformation/api-gateway-with-lambda/Makefile create mode 100644 cloudformation/api-gateway-with-lambda/README.md create mode 100644 cloudformation/api-gateway-with-lambda/main.go create mode 100644 cloudformation/api-gateway-with-lambda/template.yml diff --git a/cloudformation/api-gateway-with-lambda/.gitignore b/cloudformation/api-gateway-with-lambda/.gitignore new file mode 100644 index 0000000..fab0672 --- /dev/null +++ b/cloudformation/api-gateway-with-lambda/.gitignore @@ -0,0 +1 @@ +/bootstrap diff --git a/cloudformation/api-gateway-with-lambda/Makefile b/cloudformation/api-gateway-with-lambda/Makefile new file mode 100644 index 0000000..0ac2fe8 --- /dev/null +++ b/cloudformation/api-gateway-with-lambda/Makefile @@ -0,0 +1,28 @@ +.DEFAULT_GOAL := help + +BINARY_NAME = bootstrap +AWS_STACK_NAME = lambda-batch +TEMPLATE_FILE = template.yml + +.PHONY: help clean dependency_check build deploy +help: ## Show this help message + @grep -E '^[0-9a-zA-Z_-]+[[:blank:]]*:.*?## .*$$' $(MAKEFILE_LIST) | sort \ + | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[1;32m%-15s\033[0m %s\n", $$1, $$2}' + +clean: ## Clean project + -rm -rf $(BINARY_NAME) + +dependency_check: ## Check dependencies + @command -v sam > /dev/null 2>&1 || { echo "Error: sam is not installed. See https://github.com/aws/aws-sam-cli"; exit 1; } + +build: ## Build binary + @echo "Building for Lambda execution environment (Linux/ARM64)" + GOOS=linux GOARCH=arm64 go build -o $(BINARY_NAME) main.go + +deploy: dependency_check build ## Deploy CloudFormation Template + sam deploy --stack-name $(AWS_STACK_NAME) --template-file $(TEMPLATE_FILE) \ + --capabilities CAPABILITY_IAM --resolve-s3 --region ap-northeast-1 || { echo "SAM deployment failed"; exit 1; } + +test-deploy: build ## Deploy CloudFormation Template for test + samlocal deploy --stack-name $(AWS_STACK_NAME) --template-file $(TEMPLATE_FILE) \ + --capabilities CAPABILITY_IAM --resolve-s3 --region ap-northeast-1 || { echo "SAM deployment failed"; exit 1; } \ No newline at end of file diff --git a/cloudformation/api-gateway-with-lambda/README.md b/cloudformation/api-gateway-with-lambda/README.md new file mode 100644 index 0000000..e69de29 diff --git a/cloudformation/api-gateway-with-lambda/main.go b/cloudformation/api-gateway-with-lambda/main.go new file mode 100644 index 0000000..b4b8513 --- /dev/null +++ b/cloudformation/api-gateway-with-lambda/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "context" + "encoding/json" + "log" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" +) + +// HealthResponse struct defines the response structure +type HealthResponse struct { + Status string `json:"status"` +} + +// Handler is the Lambda function handler +func Handler(_ context.Context) (events.APIGatewayProxyResponse, error) { + // Create a response + responseBody, err := json.Marshal(HealthResponse{Status: "healthy"}) + if err != nil { + log.Printf("Error marshalling JSON response: %v", err) + return events.APIGatewayProxyResponse{StatusCode: 500}, err + } + + // Return API Gateway response + return events.APIGatewayProxyResponse{ + StatusCode: 200, + Headers: map[string]string{"Content-Type": "application/json"}, + Body: string(responseBody), + }, nil +} + +func main() { + // Start the Lambda handler + lambda.Start(Handler) +} diff --git a/cloudformation/api-gateway-with-lambda/template.yml b/cloudformation/api-gateway-with-lambda/template.yml new file mode 100644 index 0000000..3901bb4 --- /dev/null +++ b/cloudformation/api-gateway-with-lambda/template.yml @@ -0,0 +1,71 @@ +AWSTemplateFormatVersion: "2010-09-09" +Transform: + - AWS::Serverless-2016-10-31 +Description: "API Gateway with Lambda" + +Resources: + LambdaFuncttion: + Type: "AWS::Serverless::Function" + Properties: + FunctionName: lambda-function + Handler: "handler" + Runtime: provided.al2 + Architectures: [arm64] + Timeout: 10 + CodeUri: ./ + MemorySize: 128 + Policies: + - AWSLambdaBasicExecutionRole + Events: + GetApi: + Type: Api + Properties: + Path: /health + Method: get + RestApiId: !Ref API + + API: + Type: "AWS::Serverless::Api" + Properties: + Name: sam-test-get-api + EndpointConfiguration: REGIONAL + StageName: dev + + LambdaLogGroup: + Type: "AWS::Logs::LogGroup" + DeletionPolicy: Retain + UpdateReplacePolicy: Retain + Properties: + LogGroupName: !Sub "/aws/lambda/${LambdaFuncttion}" + RetentionInDays: 7 + KmsKeyId: !GetAtt LambdaLogGroupKMSKey.Arn + + LambdaLogGroupKMSKey: + Type: AWS::KMS::Key + UpdateReplacePolicy: Retain + DeletionPolicy: Retain + Properties: + Description: "KMS key for encrypting CloudWatch Logs" + EnableKeyRotation: true + KeyPolicy: + Version: "2012-10-17" + Id: "key-default" + Statement: + - Sid: "Allow administration of the key" + Effect: "Allow" + Principal: + AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root" + Action: + - "kms:*" + Resource: "*" + - Sid: "Allow use of the key" + Effect: "Allow" + Principal: + Service: "logs.ap-northeast-1.amazonaws.com" + Action: + - "kms:Encrypt" + - "kms:Decrypt" + - "kms:ReEncrypt*" + - "kms:GenerateDataKey*" + - "kms:DescribeKey" + Resource: "*" \ No newline at end of file