diff --git a/aws-lambda-tutorial/components/api-gateway/data.tf b/aws-lambda-tutorial/components/api-gateway/data.tf new file mode 100644 index 0000000..2502393 --- /dev/null +++ b/aws-lambda-tutorial/components/api-gateway/data.tf @@ -0,0 +1 @@ +data "aws_region" "current" {} diff --git a/aws-lambda-tutorial/components/api-gateway/main.tf b/aws-lambda-tutorial/components/api-gateway/main.tf index 083cbfd..27df778 100644 --- a/aws-lambda-tutorial/components/api-gateway/main.tf +++ b/aws-lambda-tutorial/components/api-gateway/main.tf @@ -30,6 +30,12 @@ module "api_gateway" { protocol_type = "HTTP" create_default_stage_access_log_group = true + default_route_settings = { + detailed_metrics_enabled = true + throttling_burst_limit = 100 + throttling_rate_limit = 100 + } + cors_configuration = { allow_headers = ["content-type", "x-amz-date", "authorization", "x-api-key", "x-amz-security-token", "x-amz-user-agent"] allow_methods = ["*"] @@ -44,3 +50,16 @@ resource "aws_lambda_permission" "permission" { principal = "apigateway.amazonaws.com" source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*" } + +resource "aws_route53_record" "custom_domain_record" { + zone_id = var.zone_id + name = var.domain_name + type = "A" + ttl = "60" + + alias { + name = module.api_gateway.apigatewayv2_domain_name_configuration[0].target_domain_name + zone_id = module.api_gateway.apigatewayv2_domain_name_configuration[0].hosted_zone_id + evaluate_target_health = false + } +} diff --git a/aws-lambda-tutorial/components/api-gateway/variables.tf b/aws-lambda-tutorial/components/api-gateway/variables.tf index 85320c5..e2506c9 100644 --- a/aws-lambda-tutorial/components/api-gateway/variables.tf +++ b/aws-lambda-tutorial/components/api-gateway/variables.tf @@ -2,6 +2,10 @@ variable "name" { type = string } +variable "lambda_function_arn" { + type = string +} + variable "domain_name" { type = string } @@ -10,6 +14,6 @@ variable "domain_name_certificate_arn" { type = string } -variable "lambda_function_arn" { +variable "zone_id" { type = string } diff --git a/aws-lambda-tutorial/components/api/internal/dynamodb/db.go b/aws-lambda-tutorial/components/api/internal/dynamodb/db.go index 8431b19..5da71d2 100644 --- a/aws-lambda-tutorial/components/api/internal/dynamodb/db.go +++ b/aws-lambda-tutorial/components/api/internal/dynamodb/db.go @@ -1,13 +1,14 @@ package dynamodb import ( + "os" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" ) -const region = "us-east-1" - -var db = dynamodb.New(session.New(), aws.NewConfig().WithRegion(region)) - -// var db = dynamodb.New(session.NewSession(), aws.NewConfig().WithRegion(region)) +var ( + region = os.Getenv("AWS_REGION") + db = dynamodb.New(session.New(), aws.NewConfig().WithRegion(region)) +) diff --git a/aws-lambda-tutorial/components/api/main.go b/aws-lambda-tutorial/components/api/main.go index 028a2ed..51a228b 100644 --- a/aws-lambda-tutorial/components/api/main.go +++ b/aws-lambda-tutorial/components/api/main.go @@ -56,8 +56,7 @@ func post(event events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, return response(http.StatusUnprocessableEntity, "", err) } - id := event.PathParameters["id"] - if _, err := strconv.Atoi(id); err != nil { + if _, err := strconv.Atoi(w.ID); err != nil { return response(http.StatusBadRequest, "", err) } diff --git a/aws-lambda-tutorial/components/lambda-function/variables.tf b/aws-lambda-tutorial/components/lambda-function/variables.tf index 187e448..4d18653 100644 --- a/aws-lambda-tutorial/components/lambda-function/variables.tf +++ b/aws-lambda-tutorial/components/lambda-function/variables.tf @@ -5,7 +5,3 @@ variable "function_name" { variable "image_uri" { type = string } - -# variable "dynamodb_table_arn" { -# type = string -# }