This repository has been archived by the owner on Apr 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathterraform.tf
80 lines (67 loc) · 2.31 KB
/
terraform.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
# Variables
variable "region" {
default = "eu-west-1"
}
variable "account_id" {}
variable "s3_bucket" {}
variable "lambda_name" {
default = "plambdapi"
}
provider "aws" {
region = "${var.region}"
}
# S3 Bucket
resource "aws_s3_bucket" "bucket" {
bucket = "${var.s3_bucket}"
acl = "public-read"
}
# API Gateway
resource "aws_api_gateway_rest_api" "api" {
name = "${var.lambda_name}_api"
binary_media_types = [
"application/zip",
"application/octet-stream",
"*/*"
]
}
resource "aws_api_gateway_resource" "api_resource" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
parent_id = "${aws_api_gateway_rest_api.api.root_resource_id}"
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "method" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${aws_api_gateway_resource.api_resource.id}"
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${aws_api_gateway_resource.api_resource.id}"
http_method = "${aws_api_gateway_method.method.http_method}"
integration_http_method = "POST"
type = "AWS_PROXY"
uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/${aws_lambda_function.lambda.arn}/invocations"
}
# Lambda
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda.arn}"
principal = "apigateway.amazonaws.com"
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
source_arn = "arn:aws:execute-api:${var.region}:${var.account_id}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method.http_method}/{proxy+}"
}
resource "aws_lambda_function" "lambda" {
filename = "lambda.zip"
function_name = "${var.lambda_name}"
role = "arn:aws:iam::032826422072:role/lambda_basic_execution"
handler = "lambda.handler"
runtime = "python3.6"
source_code_hash = "${base64sha256(file("lambda.zip"))}"
environment {
variables = {
S3_BUCKET = "${var.s3_bucket}"
}
}
}