Skip to content

Commit

Permalink
Merge branch 'main' of github.com:HappyPathway/eks-automation-lambda …
Browse files Browse the repository at this point in the history
…into main
  • Loading branch information
Dave Arnold committed Apr 23, 2025
2 parents 641c488 + b738c3f commit 0408f77
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/build-lambda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build Lambda Container

on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
workflow_dispatch:

permissions:
id-token: write
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION }}

- name: Start CodeBuild Project
run: |
BUILD_ID=$(aws codebuild start-build --project-name eks-automation-lambda-builder --output text --query 'build.id')
echo "Started build with ID: $BUILD_ID"
while true; do
STATUS=$(aws codebuild batch-get-builds --ids $BUILD_ID --query 'builds[0].buildStatus' --output text)
echo "Build status: $STATUS"
if [ "$STATUS" = "SUCCEEDED" ]; then
echo "Build completed successfully!"
exit 0
elif [ "$STATUS" = "FAILED" ] || [ "$STATUS" = "STOPPED" ] || [ "$STATUS" = "TIMED_OUT" ]; then
echo "Build failed with status: $STATUS"
exit 1
fi
sleep 30
done
25 changes: 25 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 0.2

phases:
install:
runtime-versions:
python: 3.11
commands:
- curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
- sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
- sudo apt-get update && sudo apt-get install packer

pre_build:
commands:
- echo "Initializing Packer plugins..."
- packer init .
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $REPOSITORY_URI

build:
commands:
- echo "Building the Lambda container image with Packer..."
- packer build -var="repository_uri=$REPOSITORY_URI" packer.pkr.hcl

post_build:
commands:
- echo "Build completed successfully!"
20 changes: 20 additions & 0 deletions infrastructure/github-actions-trust-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::${AWS_ACCOUNT_ID}:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:${YOUR_GITHUB_ORG}/${YOUR_REPO_NAME}:*"
},
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
}
}
}
]
}
99 changes: 99 additions & 0 deletions infrastructure/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# IAM Role for CodeBuild
resource "aws_iam_role" "codebuild" {
name = "eks-automation-lambda-codebuild-role"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "codebuild.amazonaws.com"
}
}
]
})
}

# IAM Role Policy for CodeBuild
resource "aws_iam_role_policy" "codebuild" {
name = "eks-automation-lambda-codebuild-policy"
role = aws_iam_role.codebuild.id

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Resource = ["*"]
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
},
{
Effect = "Allow"
Resource = ["*"]
Action = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"codebuild:StartBuild",
"codebuild:BatchGetBuilds",
"codebuild:StopBuild"
]
},
"ecr:CompleteLayerUpload"
]
}
]
})
}

# CodeBuild Project
resource "aws_codebuild_project" "lambda_builder" {
name = "eks-automation-lambda-builder"
service_role = aws_iam_role.codebuild.arn
build_timeout = "30"

artifacts {
type = "NO_ARTIFACTS"
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/amazonlinux2-x86_64-standard:4.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
privileged_mode = true

environment_variable {
name = "REPOSITORY_URI"
value = var.repository_uri
}
}

source {
type = "GITHUB"
location = var.github_repo_url
git_clone_depth = 1
buildspec = "buildspec.yml"
}

cache {
type = "NO_CACHE"
}

logs_config {
cloudwatch_logs {
status = "ENABLED"
}
}
}
14 changes: 14 additions & 0 deletions infrastructure/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "codebuild_project_name" {
description = "Name of the CodeBuild project"
value = aws_codebuild_project.lambda_builder.name
}

output "codebuild_project_arn" {
description = "ARN of the CodeBuild project"
value = aws_codebuild_project.lambda_builder.arn
}

output "iam_role_arn" {
description = "ARN of the IAM role used by CodeBuild"
value = aws_iam_role.codebuild.arn
}
12 changes: 12 additions & 0 deletions infrastructure/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = var.aws_region
}
15 changes: 15 additions & 0 deletions infrastructure/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "aws_region" {
description = "AWS region where resources will be created"
type = string
default = "us-west-2"
}

variable "repository_uri" {
description = "The URI of the ECR repository where the Lambda image will be pushed"
type = string
}

variable "github_repo_url" {
description = "The HTTPS clone URL of the GitHub repository"
type = string
}

0 comments on commit 0408f77

Please sign in to comment.