Skip to content

Commit c5e4b55

Browse files
committed
Lambda deployment without terraform (#115)
1 parent 9b443fc commit c5e4b55

File tree

5 files changed

+96
-141
lines changed

5 files changed

+96
-141
lines changed

.github/workflows/deploy.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: deploy
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- "handler.py"
8+
- "Pipfile*"
9+
pull_request:
10+
branches:
11+
- main
12+
paths:
13+
- "handler.py"
14+
- "Pipfile*"
15+
- ".github/workflows/deploy.yml"
16+
workflow_dispatch:
17+
concurrency:
18+
group: ${{ github.workflow }}-${{ github.ref }}
19+
cancel-in-progress: true
20+
jobs:
21+
package:
22+
if: ${{ github.event_name != 'schedule' }}
23+
runs-on: ubuntu-latest
24+
permissions:
25+
contents: read
26+
steps:
27+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
28+
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
29+
with:
30+
python-version: "3.14"
31+
architecture: "x64"
32+
cache: "pipenv"
33+
cache-dependency-path: Pipfile.lock
34+
- name: Install pipenv
35+
run: |
36+
pip install --prefer-binary -r requirements.txt
37+
pipenv install
38+
- name: Create deployment package
39+
run: |
40+
pipenv requirements > requirements.txt
41+
pip install --prefer-binary -r requirements.txt --target ${{ github.workspace }}/package
42+
cp handler.py ${{ github.workspace }}/package
43+
- name: Upload package
44+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
45+
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
46+
with:
47+
name: lambda-package
48+
path: ${{ github.workspace}}/package
49+
overwrite: true
50+
retention-days: 1
51+
if-no-files-found: error
52+
deploy:
53+
runs-on: ubuntu-latest
54+
environment: AWS
55+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
56+
needs: package
57+
permissions:
58+
id-token: write
59+
contents: read
60+
steps:
61+
- name: Download package
62+
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
63+
with:
64+
path: ${{ github.workspace }}/package
65+
- name: Configure AWS Credentials
66+
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
67+
with:
68+
aws-region: ${{ vars.AWS_REGION }}
69+
role-to-assume: ${{ secrets.AWS_ROLE }}
70+
- name: Deploy
71+
uses: aws-actions/aws-lambda-deploy@29ea35c124579506cf0475e20df36198eb670d89 # v1.1.0
72+
with:
73+
function-name: ${{ vars.LAMBDA_NAME }}
74+
package-type: Zip
75+
runtime: python3.14
76+
handler: handler.handler
77+
publish: false
78+
code-artifacts-dir: ${{ github.workspace }}/package

.github/workflows/terraform.yml

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@ on:
44
branches:
55
- main
66
paths:
7-
- "handler.py"
8-
- "create_package.py"
9-
- "Pipfile*"
10-
- "requirements.txt"
117
- "terraform/**"
128
- ".github/workflows/terraform.yml"
139
push:
1410
branches:
1511
- main
1612
paths:
17-
- "handler.py"
18-
- "create_package.py"
19-
- "Pipfile*"
2013
- "terraform/**"
2114
- ".github/workflows/terraform.yml"
2215
schedule:
@@ -26,35 +19,7 @@ concurrency:
2619
group: ${{ github.workflow }}-${{ github.ref }}
2720
cancel-in-progress: true
2821
jobs:
29-
package:
30-
if: ${{ github.event_name != 'schedule' }}
31-
runs-on: ubuntu-latest
32-
permissions:
33-
contents: read
34-
steps:
35-
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
36-
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
37-
with:
38-
python-version: "3.14"
39-
architecture: "x64"
40-
cache: "pipenv"
41-
cache-dependency-path: Pipfile.lock
42-
- name: Install pipenv & create dependencies
43-
run: |
44-
pip install --prefer-binary -r requirements.txt
45-
pipenv install
46-
pipenv requirements > requirements.txt
47-
- name: Create deployment package
48-
run: python create_package.py --output ${{ github.workspace }}/${{ vars.LAMBDA_PACKAGE }}
49-
- name: Upload package
50-
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
51-
with:
52-
name: lambda-package
53-
path: ${{ github.workspace}}/${{ vars.LAMBDA_PACKAGE }}
54-
overwrite: true
55-
retention-days: 1
56-
if-no-files-found: error
57-
terraform-lint:
22+
lint:
5823
runs-on: ubuntu-latest
5924
permissions:
6025
contents: read
@@ -74,10 +39,10 @@ jobs:
7439
id: tflint
7540
run: tflint -f compact --minimum-failure-severity notice
7641
working-directory: ./terraform
77-
terraform:
42+
deploy:
7843
if: ${{ github.event_name != 'schedule' }}
7944
runs-on: ubuntu-latest
80-
needs: [terraform-lint, package]
45+
needs: [lint]
8146
permissions:
8247
id-token: write
8348
contents: read
@@ -87,6 +52,7 @@ jobs:
8752
TF_VAR_topic_name: ${{ secrets.RSS_FEEDS_TOPIC }}
8853
TF_VAR_alarm_topic_name: ${{ secrets.RSS_FEEDS_ALARM_TOPIC }}
8954
TF_VAR_rss_feeds_urls: ${{ vars.RSS_FEEDS_URLS }}
55+
TF_VAR_lambda_name: ${{ vars.LAMBDA_NAME }}
9056
TF_VAR_lambda_package_path: ${{ github.workspace}}/${{ vars.LAMBDA_PACKAGE }}
9157
steps:
9258
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
@@ -95,10 +61,8 @@ jobs:
9561
with:
9662
aws-region: ${{ vars.AWS_REGION }}
9763
role-to-assume: ${{ secrets.AWS_ROLE }}
98-
- name: Download package
99-
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
100-
with:
101-
path: ${{ github.workspace }}
64+
- name: Create dummy package
65+
run: zip -j ${{ vars.LAMBDA_PACKAGE }} handler.py
10266
- name: Setup Terraform
10367
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
10468
- name: Terraform format

create_package.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

terraform/lambda.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_lambda_function" "rss_handler" {
2-
function_name = "rss-feeds-handler"
2+
function_name = var.lambda_name
33
role = aws_iam_role.lambda_role.arn
44
handler = "handler.handler"
55
runtime = "python3.14"
@@ -19,10 +19,14 @@ resource "aws_lambda_function" "rss_handler" {
1919

2020
filename = var.lambda_package_path
2121
source_code_hash = filebase64sha256(var.lambda_package_path)
22+
23+
lifecycle {
24+
ignore_changes = [filename, source_code_hash]
25+
}
2226
}
2327

2428
resource "aws_cloudwatch_log_group" "rss_handler_logs" {
25-
name = "/aws/lambda/rss-feeds-handler"
29+
name = "/aws/lambda/${var.lambda_name}"
2630
retention_in_days = 90
2731
}
2832

@@ -89,7 +93,7 @@ resource "aws_iam_policy" "lambda_policy" {
8993
"logs:PutLogEvents"
9094
]
9195
Resource = [
92-
"arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/rss-feeds-handler:*"
96+
"arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/${var.lambda_name}:*"
9397
]
9498
}
9599
]

terraform/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ variable "rss_feeds_urls" {
2424
type = string
2525
}
2626

27+
variable "lambda_name" {
28+
description = "Path to the Lambda deployment package"
29+
type = string
30+
}
31+
2732
variable "lambda_package_path" {
2833
description = "Path to the Lambda deployment package"
2934
type = string

0 commit comments

Comments
 (0)