-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
173 lines (146 loc) · 3.52 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
region = "us-east-1"
# shared_credentials_files = ["$HOME/.aws/credentials"]
}
# create table
resource "aws_dynamodb_table" "products" {
name = "products"
billing_mode = "PROVISIONED"
read_capacity= "1"
write_capacity= "1"
attribute {
name = "region"
type = "S"
}
hash_key = "region"
}
# create table items
resource "aws_dynamodb_table_item" "item1" {
table_name = aws_dynamodb_table.products.name
hash_key = aws_dynamodb_table.products.hash_key
item = <<ITEM
{
"region": {"S": "us"},
"product_name": {"S": "jeans_type_1"},
"id": {"S": "481ee6ce-"}
}
ITEM
}
resource "aws_dynamodb_table_item" "item2" {
table_name = aws_dynamodb_table.products.name
hash_key = aws_dynamodb_table.products.hash_key
item = <<ITEM
{
"region": {"S": "eur"},
"product_name": {"S": "jacket_type_2"},
"id": {"S": "26279956-"}
}
ITEM
}
resource "aws_dynamodb_table_item" "item3" {
table_name = aws_dynamodb_table.products.name
hash_key = aws_dynamodb_table.products.hash_key
item = <<ITEM
{
"region": {"S": "apac"},
"product_name": {"S": "shirt_type_3"},
"id": {"S": "534rrt58-"}
}
ITEM
}
# role creation
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# policy creation
resource "aws_iam_policy" "iam_policy_for_lambda" {
name = "aws_iam_policy_for_terraform_aws_lambda_role"
path = "/"
description = "AWS IAM Policy for managing aws lambda role"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
# policy attachment
resource "aws_iam_role_policy_attachment" "attach_iam_policy_to_iam_role" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = aws_iam_policy.iam_policy_for_lambda.arn
}
# archive from content
data "archive_file" "zip_the_python_code" {
type = "zip"
source_dir = "/Users/ojiakuboss/TerraformGuru/pyguy"
output_path = "/Users/ojiakuboss/TerraformGuru/pyguy/lambda_function.zip"
}
# create the Lambda function
resource "aws_lambda_function" "test_lambda" {
# If the file is not in the current working directory you will need to include a
# path.module in the filename.
filename = "/Users/ojiakuboss/TerraformGuru/pyguy/lambda_function.zip"
function_name = "lambda_function"
role = aws_iam_role.iam_for_lambda.arn
handler = "lambda_function.get_products"
runtime = "python3.9"
depends_on = [aws_iam_role_policy_attachment.attach_iam_policy_to_iam_role]
}
# create api gateway rest api
resource "aws_api_gateway_rest_api" "mbu" {
body = jsonencode({
openapi = "3.0.1"
info = {
title = "mbu"
version = "1.0"
}
paths = {
"/path1" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "HTTP_PROXY"
uri = "https://ip-ranges.amazonaws.com/ip-ranges.json"
}
}
}
}
})
name = "mbu"
endpoint_configuration {
types = ["REGIONAL"]
}
}