Complete the code to define a serverless function using AWS Lambda.
resource "aws_lambda_function" "example" { function_name = "my_lambda_function" runtime = "python3.9" handler = "handler.[1]" role = aws_iam_role.lambda_exec.arn filename = "lambda_function.zip" }
The handler specifies the function AWS Lambda calls to start execution. The default in Python is often lambda_handler.
Complete the code to define a container task in AWS ECS.
resource "aws_ecs_task_definition" "example" { family = "my_task" network_mode = "awsvpc" requires_compatibilities = ["FARGATE"] cpu = "256" memory = "512" container_definitions = jsonencode([ { name = "my_container", image = "nginx:latest", essential = true, portMappings = [ { containerPort = [1], hostPort = 80, protocol = "tcp" } ] } ]) }
The container port should match the port the container listens on. For nginx, the default is port 80.
Fix the error in the serverless function timeout setting.
resource "aws_lambda_function" "example" { function_name = "my_lambda_function" runtime = "python3.9" handler = "handler.lambda_handler" role = aws_iam_role.lambda_exec.arn filename = "lambda_function.zip" timeout = [1] }
AWS Lambda timeout maximum is 900 seconds (15 minutes). Setting timeout to 1000 or 0 causes errors.
Fill both blanks to configure an ECS service with desired count and launch type.
resource "aws_ecs_service" "example" { name = "my_service" cluster = aws_ecs_cluster.example.id task_definition = aws_ecs_task_definition.example.arn desired_count = [1] launch_type = "[2]" }
The desired_count sets how many tasks run. Launch_type 'FARGATE' runs containers serverlessly without managing EC2 instances.
Fill all three blanks to create a serverless API Gateway integration with Lambda.
resource "aws_api_gateway_integration" "example" { rest_api_id = aws_api_gateway_rest_api.example.id resource_id = aws_api_gateway_resource.example.id http_method = aws_api_gateway_method.example.http_method integration_http_method = "[1]" type = "[2]" uri = aws_lambda_function.example.[3] }
API Gateway integration with Lambda uses HTTP method POST, type AWS_PROXY, and the Lambda function's invoke_arn.