-
Notifications
You must be signed in to change notification settings - Fork 0
add initial terraform implementation #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Rayna-Yu
wants to merge
8
commits into
main
Choose a base branch
from
159-create-lambda-and-api-gateway-terraform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1028c71
add initial terraform implementation
Rayna-Yu f4df4ed
chore: auto-format terraform and update documentation
github-actions[bot] c13084c
address comments
Rayna-Yu 9246545
Merge branch 'main' into 159-create-lambda-and-api-gateway-terraform
Rayna-Yu a42a166
fix rules to rule
Rayna-Yu b07608b
redo path
Rayna-Yu e332149
fix?
Rayna-Yu 56f211f
try nondynamic?
Rayna-Yu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,108 @@ | ||
| # IAM role for Lambda functions | ||
| resource "aws_iam_role" "lambda_role" { | ||
| name = "branch-lambda-role" | ||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [{ | ||
| Action = "sts:AssumeRole" | ||
| Effect = "Allow" | ||
| Principal = { Service = "lambda.amazonaws.com" } | ||
| }] | ||
| }) | ||
| } | ||
|
|
||
| # Attach basic execution policy for CloudWatch Logs | ||
| resource "aws_iam_role_policy_attachment" "lambda_basic" { | ||
| role = aws_iam_role.lambda_role.name | ||
| policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
| } | ||
|
|
||
| # Get AWS account ID for unique bucket naming | ||
| data "aws_caller_identity" "current" {} | ||
|
|
||
| resource "aws_s3_bucket" "lambda_deployments" { | ||
| bucket = "branch-lambda-deployments-${data.aws_caller_identity.current.account_id}" | ||
| } | ||
|
|
||
| resource "aws_s3_bucket_versioning" "lambda_deployments" { | ||
| bucket = aws_s3_bucket.lambda_deployments.id | ||
|
|
||
| versioning_configuration { | ||
| status = "Enabled" | ||
| } | ||
| } | ||
|
|
||
| resource "aws_s3_bucket_server_side_encryption_configuration" "lambda_deployments" { | ||
| bucket = aws_s3_bucket.lambda_deployments.id | ||
|
|
||
| rule { | ||
| apply_server_side_encryption_by_default { | ||
| sse_algorithm = "AES256" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Define all Lambda functions | ||
| locals { | ||
| lambda_functions = toset([ | ||
| "auth", | ||
| "donors", | ||
| "expenditures", | ||
| "projects", | ||
| "reports", | ||
| "users", | ||
| ]) | ||
| } | ||
|
|
||
| # Minimal placeholder that will be replaced by GitHub Actions on first deployment | ||
| data "archive_file" "lambda_placeholder" { | ||
| type = "zip" | ||
| output_path = "${path.module}/lambda-placeholder.zip" | ||
| source { | ||
| content = "exports.handler = async () => ({ statusCode: 200, body: JSON.stringify({ message: 'Placeholder - will be replaced by CI/CD' }) });" | ||
| filename = "handler.js" | ||
| } | ||
| } | ||
|
|
||
| # This allows Terraform to create the Lambda functions initially | ||
| resource "aws_s3_object" "lambda_placeholder" { | ||
| for_each = local.lambda_functions | ||
|
|
||
| bucket = aws_s3_bucket.lambda_deployments.id | ||
| key = "${each.key}/initial.zip" | ||
| source = data.archive_file.lambda_placeholder.output_path | ||
|
|
||
| content_type = "application/zip" | ||
| } | ||
|
|
||
| # Create all Lambda functions with a single resource block | ||
| resource "aws_lambda_function" "functions" { | ||
| for_each = local.lambda_functions | ||
|
|
||
| function_name = "branch-${each.key}" | ||
| runtime = "nodejs20.x" | ||
| handler = "handler.handler" | ||
| timeout = 30 | ||
| memory_size = 256 | ||
| role = aws_iam_role.lambda_role.arn | ||
|
|
||
| # Use S3 for deployment (initial placeholder, replaced by GitHub Actions) | ||
| s3_bucket = aws_s3_bucket.lambda_deployments.id | ||
| s3_key = aws_s3_object.lambda_placeholder[each.key].key | ||
|
|
||
| # Prevent Terraform from reverting code deployments made by GitHub Actions | ||
| lifecycle { | ||
| ignore_changes = [s3_key] | ||
| } | ||
|
|
||
| environment { | ||
| variables = { | ||
| NODE_ENV = "production" | ||
| DB_HOST = aws_db_instance.branch_rds.address | ||
| DB_USER = data.infisical_secrets.rds_folder.secrets["username"].value | ||
| DB_PASSWORD = data.infisical_secrets.rds_folder.secrets["password"].value | ||
| DB_PORT = try(data.infisical_secrets.rds_folder.secrets["db_port"].value, "5432") | ||
| DB_NAME = try(data.infisical_secrets.rds_folder.secrets["db_name"].value, aws_db_instance.branch_rds.db_name) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also just wanting to confirm this is correct arn for the aws managed role
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is correct: https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AWSLambdaBasicExecutionRole.html and got it from the lambda deployment guide: https://www.notion.so/Research-Lambda-Deployment-81-2a899c51d64f80dc930bf16bb0e43a97
But just want to double confirm this is what you are asking.