-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
107 lines (94 loc) · 2.65 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
terraform {
required_providers {
aws = {
version = ">= 4.0.0"
source = "hashicorp/aws"
}
}
}
# specify the provider region
provider "aws" {
region = "ca-central-1"
}
# the locals block is used to declare constants that
# you can use throughout your code
locals {
function_name = "hello-world"
handler_name = "main.handler"
artifact_name = "${local.function_name}/artifact.zip"
}
# create a role for the Lambda function to assume
# every service on AWS that wants to call other AWS services should first assume a role.
# then any policy attached to the role will give permissions
# to the service so it can interact with other AWS services
# see the docs: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
resource "aws_iam_role" "lambda" {
name = "iam-for-lambda-${local.function_name}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# create a policy for publishing logs to CloudWatch
# see the docs: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy
resource "aws_iam_policy" "logs" {
name = "lambda-logging-${local.function_name}"
description = "IAM policy for logging from a lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}
]
}
EOF
}
# attach the above policy to the function role
# see the docs: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.logs.arn
}
resource "aws_dynamodb_table" "lotion-30147402" {
name = "lotion-30147402"
billing_mode = "PROVISIONED"
# up to 8KB read per second (eventually consistent)
read_capacity = 1
# up to 1KB per second
write_capacity = 1
# we only need a student id to find an item in the table; therefore, we
# don't need a sort key here
hash_key = "email"
range_key = "noteID"
# the hash_key data type is string
attribute {
name = "email"
type = "S"
}
attribute {
name = "noteID"
type = "S"
}
}
//aws-vault exec {ishahaiderr or nessma} -- terraform init
//aws-vault exec {ishahaiderr or nessma} -- terraform plan
//aws-vault exec {ishahaiderr or nessma} -- terraform apply