infra: add tagging to resources (#17)

This commit is contained in:
Marc 2022-10-09 12:42:18 -04:00 committed by GitHub
parent 51400dc45f
commit b46a200226
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 20 deletions

View file

@ -0,0 +1,15 @@
locals {
service_name = "lambda-boilerplate"
}
locals {
service_longname = "${var.env_name}_${local.service_name}"
}
locals {
common_tags = {
stack_name = local.service_longname
environment_name = var.env_name
commit_sha = var.commit_sha
}
}

View file

@ -1,18 +1,5 @@
terraform {
required_version = ">=1.0"
required_providers {
aws = "4.34.0"
}
}
provider "aws" {
profile = "default"
region = var.aws_region
}
resource "aws_iam_role" "lambda_role" {
name = "lambda_role"
name = "${local.service_longname}_lambda-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
@ -28,21 +15,29 @@ resource "aws_iam_role" "lambda_role" {
]
}
EOF
tags = local.common_tags
}
resource "aws_lambda_function" "lambda_func" {
function_name = "boilerplate_function"
function_name = "${local.service_longname}_function"
role = aws_iam_role.lambda_role.arn
handler = "src.base.handler"
runtime = "python3.8"
s3_bucket = var.artifacts_bucket_name
s3_key = var.lambda_archive_name
tags = local.common_tags
}
resource "aws_api_gateway_rest_api" "gateway" {
name = "boilerplate"
name = "${local.service_longname}_gateway"
description = "Lambda Boilerplate"
tags = local.common_tags
}
resource "aws_api_gateway_resource" "lambda_proxy" {
@ -66,6 +61,7 @@ resource "aws_api_gateway_integration" "lambda" {
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
}
resource "aws_api_gateway_deployment" "lambda" {
@ -75,6 +71,7 @@ resource "aws_api_gateway_deployment" "lambda" {
rest_api_id = aws_api_gateway_rest_api.gateway.id
stage_name = "test"
}
resource "aws_lambda_permission" "apigw" {
@ -83,6 +80,7 @@ resource "aws_lambda_permission" "apigw" {
function_name = aws_lambda_function.lambda_func.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.gateway.execution_arn}/*/*"
}
output "base_url" {

View file

@ -0,0 +1,13 @@
terraform {
required_version = ">=1.0"
required_providers {
aws = "4.34.0"
}
}
provider "aws" {
profile = "default"
region = var.aws_region
}

View file

@ -2,6 +2,14 @@ variable "aws_region" {
type = string
}
variable "commit_sha" {
type = string
}
variable "env_name" {
type = string
}
variable "artifacts_bucket_name" {
type = string
}

View file

@ -9,7 +9,7 @@
source $(dirname $0)/../.config
PROJECT_ROOT=$(realpath $BOOTSTRAP_ROOT/infrastructure/$PROJECT)
DEFAULT_ENVNAME="dev-$USER"
cd $PROJECT_ROOT
terraform init
@ -27,5 +27,5 @@
terraform taint --allow-missing $RESOURCE
done
terraform apply --var-file $VARIABLES_PATH
terraform apply --var-file $VARIABLES_PATH -var="env_name=${ENV_NAME:-$DEFAULT_ENVNAME}" -var="commit_sha=$(git log --pretty=format:'%H' -n 1)"
)

View file

@ -9,9 +9,9 @@
source $(dirname $0)/../.config
PROJECT_ROOT=$(realpath $BOOTSTRAP_ROOT/infrastructure/$PROJECT)
DEFAULT_ENVNAME="dev-$USER"
cd $PROJECT_ROOT
terraform init
terraform plan --var-file $VARIABLES_PATH
terraform plan --var-file $VARIABLES_PATH -var="env_name=${ENV_NAME:-$DEFAULT_ENVNAME}" -var="commit_sha=$(git log --pretty=format:'%H' -n 1)"
)