Skip to content

Commit ef446ab

Browse files
committed
fix: pre-create EC2 Spot SLR and grant Lambda permission to create it
- Add aws_iam_service_linked_role for spot.amazonaws.com in ec2.tf so the AWSServiceRoleForEC2Spot role exists before any Spot launch attempt - Grant lambda_process iam:CreateServiceLinkedRole as a runtime fallback - Fix MessageGroupId=None error in submit_build Lambda (non-FIFO queue)
1 parent e58d471 commit ef446ab

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

infrastructure/infra.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```shell
2+
api_url = "https://9nij0o9osa.execute-api.eu-central-1.amazonaws.com"
3+
check_status_lambda = "lambda-layer-builder-prod-check-status"
4+
dynamodb_table_name = "lambda-layer-builder-prod-builds"
5+
github_pages_config = "API URL: https://9nij0o9osa.execute-api.eu-central-1.amazonaws.com"
6+
process_build_lambda = "lambda-layer-builder-prod-process-build"
7+
s3_bucket_name = "lambda-layer-builder-prod-artifacts-4b4dd9b4"
8+
sqs_queue_url = "https://sqs.eu-central-1.amazonaws.com/511637446646/lambda-layer-builder-prod-build-queue"
9+
submit_build_lambda = "lambda-layer-builder-prod-submit-build"
10+
vpc_id = "vpc-06d62836cc88378ad"
11+
```

infrastructure/terraform/ec2.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66
# Each instance self-terminates after build completion or timeout.
77
# =============================================================================
88

9+
# Pre-create the EC2 Spot service-linked role so Lambda doesn't need to create it at runtime.
10+
# This role is account-global and only needs to exist once.
11+
# Terraform will silently import it if it already exists.
12+
resource "aws_iam_service_linked_role" "ec2_spot" {
13+
aws_service_name = "spot.amazonaws.com"
14+
description = "Service-linked role for EC2 Spot Instances"
15+
16+
# Ignore if this role already exists in the account
17+
lifecycle {
18+
ignore_changes = [description]
19+
}
20+
}
21+
922
# Latest Amazon Linux 2023 AMI
1023
data "aws_ami" "al2023" {
1124
most_recent = true

infrastructure/terraform/iam.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ resource "aws_iam_role_policy" "lambda_process" {
105105
]
106106
Resource = "*"
107107
},
108+
{
109+
# Required to create AWSServiceRoleForEC2Spot on first Spot usage
110+
Effect = "Allow"
111+
Action = "iam:CreateServiceLinkedRole"
112+
Resource = "arn:aws:iam::*:role/aws-service-role/spot.amazonaws.com/AWSServiceRoleForEC2Spot"
113+
Condition = {
114+
StringLike = {
115+
"iam:AWSServiceName" = "spot.amazonaws.com"
116+
}
117+
}
118+
},
108119
{
109120
Effect = "Allow"
110121
Action = "iam:PassRole"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# =============================================================================
2+
# Lambda Python Layer Builder - Terraform Configuration
3+
# =============================================================================
4+
# Copy this file to terraform.tfvars and customize for your environment.
5+
# =============================================================================
6+
7+
# AWS region for all resources
8+
aws_region = "eu-central-1"
9+
10+
# Environment name
11+
environment = "prod"
12+
13+
# Project name (used as prefix for all resources)
14+
project_name = "lambda-layer-builder"
15+
16+
# Hours to keep build artifacts in S3 (1-168)
17+
artifact_ttl_hours = 24
18+
19+
# Docker image prefix for pre-built images
20+
# docker_image_prefix = "ghcr.io/fok666/lambda-python-layer"
21+
22+
# GitHub repo URL (fallback for local Docker builds)
23+
# github_repo_url = "https://github.com/fok666/lambda-python-layer.git"
24+
25+
# EC2 Spot instance type for builds
26+
# c5.xlarge = 4 vCPU, 8GB (~$0.04/hr spot) - Recommended
27+
# c5.2xlarge = 8 vCPU, 16GB (~$0.08/hr spot) - Heavy builds
28+
# m5.large = 2 vCPU, 8GB (~$0.02/hr spot) - Light builds
29+
ec2_instance_type = "c5.xlarge"
30+
31+
# EBS volume size in GB (30-200)
32+
ec2_volume_size = 50
33+
34+
# Max build time before instance self-terminates (safety net)
35+
ec2_max_build_time_minutes = 30
36+
37+
# CORS origins - restrict to your GitHub Pages URL in production
38+
# Example: ["https://yourusername.github.io"]
39+
allowed_origins = ["*"]
40+
41+
# API request limits
42+
# api_throttle_rate = 10
43+
# api_throttle_burst = 20

0 commit comments

Comments
 (0)