forked from kj4c/Alphas_Propex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
98 lines (81 loc) · 2.55 KB
/
main.tf
File metadata and controls
98 lines (81 loc) · 2.55 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
provider "aws" {
region = var.aws_region
}
# S3 Bucket for deployment artifacts
#resource "aws_s3_bucket" "lambda_bucket" {
# bucket = var.lambda_bucket_name
#}
# S3 Bucket for RAW DATA
resource "aws_s3_bucket" "raw_data_bucket" {
bucket = "alpha-raw-data-bucket"
tags = {
Name = "JSON Raw Data Storage Bucket"
Environment = "Dev"
}
}
#Create ECR Repository for Lambda Docker Images
resource "aws_ecr_repository" "lambda_repo" {
name = "docker-lambda"
}
# AWS Lambda Function using Docker Image
resource "aws_lambda_function" "multi_lambda" {
for_each = var.lambda_functions
function_name = each.key
package_type = "Image"
image_uri = "754754248018.dkr.ecr.us-east-1.amazonaws.com/docker-lambda@sha256:f2d4b8eebadc30c43ea383b8e833a22971edd7f809f3c312817cf60245782020"
role = "arn:aws:iam::754754248018:role/LabRole"
timeout = 900
memory_size = 512
environment {
variables = {
FUNCTION_NAME = each.key
}
}
}
# API Gateway
resource "aws_apigatewayv2_api" "api" {
name = "alpha_api"
protocol_type = "HTTP"
cors_configuration {
allow_headers = ["*"]
allow_methods = ["GET", "POST", "DELETE", "PUT", "OPTIONS"]
allow_origins = ["*"]
}
}
resource "aws_apigatewayv2_integration" "lambda_integration" {
for_each = var.lambda_functions
api_id = aws_apigatewayv2_api.api.id
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.multi_lambda[each.key].invoke_arn
integration_method = "POST"
payload_format_version = "2.0"
}
resource "aws_apigatewayv2_route" "lambda_route" {
for_each = var.lambda_functions
api_id = aws_apigatewayv2_api.api.id
route_key = "${each.value.method} /${each.key}"
target = "integrations/${aws_apigatewayv2_integration.lambda_integration[each.key].id}"
}
resource "aws_apigatewayv2_stage" "api_stage" {
api_id = aws_apigatewayv2_api.api.id
name = "$default"
auto_deploy = true
}
# gives the lambda functions permissions
resource "aws_lambda_permission" "api_gateway_invoke" {
for_each = var.lambda_functions
statement_id = "${each.key}-AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.multi_lambda[each.key].function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.api.execution_arn}/*/*"
}
terraform {
backend "s3" {
bucket = "seng3011-alpha-terraform-state"
key = "terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-lock"
}
}