-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
116 lines (100 loc) · 3.63 KB
/
main.tf
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
108
109
110
111
112
113
114
115
116
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
# Create archive from the code present in main.pys
data "archive_file" "string_replace_lambda" {
type = "zip"
source_file = "function.py"
output_path = "string-replace.zip"
}
# Creating s3 bucket
resource "aws_s3_bucket" "lambda_bucket" {
bucket = "lambda-str-bucket"
acl = "private"
}
# Store lambda zip object in S3
resource "aws_s3_bucket_object" "string_replace_lambda_s3" {
bucket = aws_s3_bucket.lambda_bucket.id
key = "string-replace.zip"
source = data.archive_file.string_replace_lambda.output_path
etag = filemd5(data.archive_file.string_replace_lambda.output_path)
}
# Creating lambda function in aws
resource "aws_lambda_function" "string_replace" {
function_name = "StringReplace"
s3_bucket = aws_s3_bucket.lambda_bucket.id
s3_key = aws_s3_bucket_object.string_replace_lambda_s3.key
runtime = "python3.9"
handler = "function.lambda_handler"
source_code_hash = data.archive_file.string_replace_lambda.output_base64sha256
role = aws_iam_role.lambda_exec.arn
}
# Providing minimum required access from IAM policy
resource "aws_iam_role" "lambda_exec" {
name = "string_replace_lambda"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_api_gateway_rest_api" "string_replace_api_gw" {
name = "string_relace_api"
}
resource "aws_api_gateway_resource" "replace_resource" {
parent_id = aws_api_gateway_rest_api.string_replace_api_gw.root_resource_id
path_part = "replace"
rest_api_id = aws_api_gateway_rest_api.string_replace_api_gw.id
}
resource "aws_api_gateway_method" "post_replace" {
resource_id = aws_api_gateway_resource.replace_resource.id
rest_api_id = aws_api_gateway_rest_api.string_replace_api_gw.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integrate_function_api" {
http_method = aws_api_gateway_method.post_replace.http_method
resource_id = aws_api_gateway_resource.replace_resource.id
rest_api_id = aws_api_gateway_rest_api.string_replace_api_gw.id
uri = "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/${aws_lambda_function.string_replace.arn}/invocations"
type = "AWS_PROXY"
integration_http_method = "POST" #for AWs only POST method supported by Terraform
depends_on = [
aws_lambda_function.string_replace,
]
}
resource "aws_api_gateway_deployment" "replace_st" {
rest_api_id = aws_api_gateway_rest_api.string_replace_api_gw.id
depends_on = [
aws_api_gateway_integration.integrate_function_api,
]
}
resource "aws_api_gateway_stage" "prod_stage" {
deployment_id = aws_api_gateway_deployment.replace_st.id
rest_api_id = aws_api_gateway_rest_api.string_replace_api_gw.id
stage_name = "prod"
}
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.string_replace.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.string_replace_api_gw.execution_arn}/*/*"
}
output "base_url" {
description = "Base URL for API Gateway stage."
value = aws_api_gateway_stage.prod_stage.invoke_url
}