-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates 2024-07-10 - Terraform deploy prep
- Loading branch information
1 parent
3a25d4b
commit cccba39
Showing
7 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ jobs: | |
cd ./herding_cats_pipelines/lambda_jobs | ||
pip install -r requirements.txt -t ./package | ||
cp *.py ./package/ | ||
cd ./package && zip -r ../lambda_jobs.zip . | ||
cd ./package && zip -r ../lambda_herding_cats_jobs.zip . | ||
- name: Configure AWS credentials | ||
uses: aws-actions/[email protected] | ||
with: | ||
|
@@ -41,4 +41,4 @@ jobs: | |
- name: Upload to S3 | ||
run: | | ||
cd ./herding_cats_pipelines/lambda_jobs | ||
aws s3 cp lambda_jobs.zip s3://${{ secrets.S3_BUCKET_NAME }}/lambda_jobs.zip | ||
aws s3 cp lambda_herding_cats_jobs.zip s3://${{ secrets.S3_BUCKET_NAME }}/lambda_herding_cats_jobs.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
provider "aws" { | ||
region = var.aws_region | ||
} | ||
|
||
# LAMBDA FUNCTION | ||
resource "aws_lambda_function" "herding-cats" { | ||
function_name = var.function_name | ||
role = aws_iam_role.lambda_role.arn | ||
handler = "main.lambda_handler" | ||
memory_size = 2048 | ||
reserved_concurrent_executions = 1 | ||
timeout = 15 | ||
runtime = "python3.11" | ||
s3_bucket = var.code_bucket_name | ||
s3_key = "lambda_herding_cats_jobs.zip" | ||
source_code_hash = data.aws_s3_object.lambda_code.etag | ||
} | ||
|
||
# IAM role for Lambda | ||
resource "aws_iam_role" "lambda_role" { | ||
name = "${var.function_name}-role" | ||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [{ | ||
Action = "sts:AssumeRole" | ||
Effect = "Allow" | ||
Principal = { | ||
Service = "lambda.amazonaws.com" | ||
} | ||
}] | ||
}) | ||
} | ||
|
||
# Lambda basic execution policy | ||
resource "aws_iam_role_policy_attachment" "lambda_basic_execution" { | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
role = aws_iam_role.lambda_role.name | ||
} | ||
|
||
# S3 read policy for code bucket | ||
resource "aws_iam_policy" "s3_code_access_policy" { | ||
name = "${var.function_name}-s3-code-access-policy" | ||
path = "/" | ||
description = "IAM policy for S3 read access to code bucket from Lambda" | ||
policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [{ | ||
Effect = "Allow" | ||
Action = [ | ||
"s3:GetObject", | ||
"s3:ListBucket" | ||
] | ||
Resource = [ | ||
"arn:aws:s3:::${var.code_bucket_name}", | ||
"arn:aws:s3:::${var.code_bucket_name}/*" | ||
] | ||
}] | ||
}) | ||
} | ||
|
||
# S3 read/write policy for the data bucket | ||
resource "aws_iam_policy" "s3_data_access_policy" { | ||
name = "${var.function_name}-s3-data-access-policy" | ||
path = "/" | ||
description = "IAM policy for S3 read and write access to the data bucket from Lambda" | ||
policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [{ | ||
Effect = "Allow" | ||
Action = [ | ||
"s3:GetObject", | ||
"s3:PutObject", | ||
"s3:DeleteObject", | ||
"s3:ListBucket" | ||
] | ||
Resource = [ | ||
"arn:aws:s3:::${var.data_bucket_name}", | ||
"arn:aws:s3:::${var.data_bucket_name}/*" | ||
] | ||
}] | ||
}) | ||
} | ||
|
||
# Attach S3 code access policy to Lambda role | ||
resource "aws_iam_role_policy_attachment" "lambda_s3_code_access" { | ||
policy_arn = aws_iam_policy.s3_code_access_policy.arn | ||
role = aws_iam_role.lambda_role.name | ||
} | ||
|
||
# Attach S3 data access policy to Lambda role | ||
resource "aws_iam_role_policy_attachment" "lambda_s3_data_access" { | ||
policy_arn = aws_iam_policy.s3_data_access_policy.arn | ||
role = aws_iam_role.lambda_role.name | ||
} | ||
|
||
|
||
data "aws_s3_object" "lambda_code" { | ||
bucket = var.code_bucket_name | ||
key = "lambda_herding_cats_jobs.zip" | ||
} |
19 changes: 19 additions & 0 deletions
19
herding_cats_pipelines/terraform/lambda-module/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
variable "aws_region" { | ||
description = "The AWS region to deploy to" | ||
type = string | ||
} | ||
|
||
variable "function_name" { | ||
description = "The name of the Lambda function" | ||
type = string | ||
} | ||
|
||
variable "code_bucket_name" { | ||
description = "Name of the existing S3 bucket containing Lambda function code" | ||
type = string | ||
} | ||
|
||
variable "data_bucket_name" { | ||
description = "Name of the S3 bucket for Lambda function data operations" | ||
type = string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters