-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.tf
More file actions
107 lines (94 loc) · 2.7 KB
/
Copy pathmain.tf
File metadata and controls
107 lines (94 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
archive = {
source = "hashicorp/archive"
version = "~> 2.4"
}
}
}
variable "provider_endpoint" {
description = "AWS API endpoint URL for Terraform"
type = string
default = "http://localhost:8000"
}
variable "lambda_endpoint" {
description = "AWS API endpoint URL for the Lambda function"
type = string
default = "http://localhost:8000"
}
provider "aws" {
region = "us-east-1"
access_key = "test"
secret_key = "test"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
s3_use_path_style = true
endpoints {
s3 = var.provider_endpoint
lambda = var.provider_endpoint
iam = var.provider_endpoint
}
}
# 1. Create the S3 Bucket
resource "aws_s3_bucket" "upload_bucket" {
bucket = "incoming-uploads"
}
# 2. Package the Lambda function
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "bootstrap"
output_path = "function.zip"
}
# 3. Dummy IAM Role (Required by Terraform, ignored by Gopherstack)
resource "aws_iam_role" "lambda_exec" {
name = "s3-lambda-exec-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
}
# 4. Create the Lambda Function
resource "aws_lambda_function" "processor" {
function_name = "s3-processor"
role = aws_iam_role.lambda_exec.arn
handler = "bootstrap"
runtime = "provided.al2"
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
environment {
variables = {
AWS_ENDPOINT_URL = var.lambda_endpoint
AWS_ACCESS_KEY_ID = "test"
AWS_SECRET_ACCESS_KEY = "test"
AWS_DEFAULT_REGION = "us-east-1"
}
}
}
# 5. Grant S3 permission to invoke the Lambda
resource "aws_lambda_permission" "allow_bucket" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.processor.arn
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.upload_bucket.arn
}
# 6. Configure the S3 Bucket Notification
resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = aws_s3_bucket.upload_bucket.id
lambda_function {
lambda_function_arn = aws_lambda_function.processor.arn
events = ["s3:ObjectCreated:*"]
}
depends_on = [aws_lambda_permission.allow_bucket]
}