Skip to content

Commit 1a19ac6

Browse files
author
wuychloe@amazon.com chloe1818
committed
Initial commit
1 parent 277fdfd commit 1a19ac6

21 files changed

+700
-1209
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ dist
33
.DS_Store
44
coverage
55
repolinter
6-
deploy-lambda.yml
76
Config

README.md

Lines changed: 110 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Updates the code and configuration of AWS Lambda functions
77
<!-- toc -->
88

99
- [Usage](#usage)
10+
* [Update Function Configuration](#update-configuration-only)
1011
* [Using S3 Deployment Method](#using-s3-deployment-method)
11-
* [Update Configuration Only](#update-configuration-only)
1212
* [Dry Run Mode](#dry-run-mode)
13+
- [Build from Source](#build-from-source)
1314
- [Inputs](#inputs)
1415
- [Outputs](#outputs)
1516
- [Credentials and Region](#credentials-and-region)
16-
* [OpenID Connect (OIDC) - Recommended Approach](#openid-connect-oidc---recommended-approach)
1717
- [Permissions](#permissions)
1818
- [License Summary](#license-summary)
1919
- [Security Disclosures](#security-disclosures)
@@ -23,124 +23,142 @@ Updates the code and configuration of AWS Lambda functions
2323
## Usage
2424

2525
```yaml
26-
name: Deploy Lambda Function
26+
name: Deploy to AWS Lambda
2727

2828
on:
2929
push:
30-
branches: [main, master]
30+
branches: [ "main" ]
31+
32+
permissions:
33+
id-token: write # This is required for OIDC authentication
34+
contents: read # This is required to checkout the repository
3135

3236
jobs:
3337
deploy:
38+
name: Deploy
3439
runs-on: ubuntu-latest
35-
permissions:
36-
id-token: write # Required for OIDC authentication
37-
contents: read # Required to check out the repository
40+
environment: production
41+
3842
steps:
39-
- uses: actions/checkout@v3
40-
41-
- name: Configure AWS credentials
42-
uses: aws-actions/configure-aws-credentials@v2
43-
with:
44-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
45-
- name: Deploy Lambda function
46-
uses: aws-actions/amazon-lambda-deploy@v1
47-
with:
48-
function-name: my-lambda-function
49-
code-artifacts-dir: ./dist
43+
- name: Checkout
44+
uses: actions/checkout@v4
45+
46+
- name: Configure AWS credentials
47+
uses: aws-actions/configure-aws-credentials@v3
48+
with:
49+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
50+
aws-region: ${{ env.AWS_REGION }}
51+
# The role-to-assume should be the ARN of the IAM role you created for GitHub Actions OIDC
52+
53+
- name: Deploy Lambda Function
54+
uses: aws-actions/aws-lambda-deploy@v1
55+
with:
56+
function-name: my-function-name
57+
code-artifacts-dir: my-code-artifacts-dir
58+
# handler: my-handler
59+
# runtime: my-runtime
60+
# Add any additional inputs your action supports
5061
```
5162

52-
### Using S3 Deployment Method
63+
The required parameters to deploy are function name, code artifacts directory, handler, and runtime. The function name and code artifacts directory need to be provided by the user. However, the handler and runtime do not and will default to index.handler and nodejs20.x if not provided.
64+
65+
### Update Function Configuration
5366

5467
```yaml
55-
name: Deploy Lambda Function with S3
68+
- name: Update Lambda configuration
69+
uses: aws-actions/aws-lambda-deploy@v1
70+
with:
71+
function-name: my-function-name
72+
code-artifacts-dir: my-code-artifacts-dir
73+
memory-size: 512
74+
timeout: 60
75+
environment: '{"ENV":"production","DEBUG":"true"}'
76+
```
5677
57-
on:
58-
push:
59-
branches: [main, master]
78+
### Using S3 Deployment Method
6079
61-
jobs:
62-
deploy:
63-
runs-on: ubuntu-latest
64-
permissions:
65-
id-token: write # Required for OIDC authentication
66-
contents: read # Required to check out the repository
67-
steps:
68-
- uses: actions/checkout@v3
69-
70-
- name: Configure AWS credentials with OIDC
71-
uses: aws-actions/configure-aws-credentials@v2
72-
with:
73-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
74-
80+
```yaml
7581
- name: Deploy Lambda function via S3
76-
uses: aws-actions/amazon-lambda-deploy@v1
82+
uses: aws-actions/aws-lambda-deploy@v1
7783
with:
78-
function-name: my-lambda-function
79-
code-artifacts-dir: ./dist
80-
s3-bucket: my-lambda-deployment-bucket
84+
function-name: my-function-name
85+
code-artifacts-dir: my-code-artifacts-dir
86+
s3-bucket: my-s3-bucket
8187
# s3-key is optional - a key will be auto-generated if not specified
8288
```
8389

84-
### Update Configuration Only
90+
### Dry Run Mode
8591

8692
```yaml
87-
name: Update Lambda Configuration
93+
- name: Deploy on dry run mode
94+
uses: aws-actions/aws-lambda-deploy@v1
95+
with:
96+
function-name: my-function-name
97+
code-artifacts-dir: my-code-artifacts-dir
98+
dry-run: true
99+
```
100+
## Build from Source
88101
89-
on:
90-
push:
91-
branches: [main, master]
102+
To automate building your source code, add the step that corresponds to your runtime:
92103
93-
jobs:
94-
deploy:
95-
runs-on: ubuntu-latest
96-
permissions:
97-
id-token: write # Required for OIDC authentication
98-
contents: read # Required to check out the repository
99-
steps:
100-
- uses: actions/checkout@v3
101-
102-
- name: Configure AWS credentials with OIDC
103-
uses: aws-actions/configure-aws-credentials@v2
104-
with:
105-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
106-
- name: Update Lambda configuration
107-
uses: aws-actions/amazon-lambda-deploy@v1
108-
with:
109-
function-name: my-lambda-function
110-
code-artifacts-dir: ./dist
111-
memory-size: 512
112-
timeout: 60
113-
environment: '{"ENV":"production","DEBUG":"true"}'
104+
### Node.js
105+
106+
```yaml
107+
- name: Build source code
108+
run: |
109+
# Install dependencies
110+
npm ci
111+
112+
# Build
113+
npm run build
114114
```
115+
### Python
115116
116-
### Dry Run Mode
117+
```yaml
118+
- name: Build source code using setup tools
119+
run: |
120+
# Install dependencies
121+
pip install -r requirement.txt
122+
123+
# Build
124+
python -m build
125+
```
126+
127+
### Ruby
117128
118129
```yaml
119-
name: Validate Lambda Deployment
130+
- name: Build source code using Rake
131+
run: |
132+
# Install dependencies
133+
bundle install
134+
135+
# Build
136+
bundle exec rake [task_name]
137+
```
120138
121-
on:
122-
pull_request:
123-
branches: [main, master]
139+
### Java
140+
141+
```yaml
142+
- name: Build source code using Maven
143+
run: |
144+
# Install dependencies
145+
mvn dependency:resolve clean install -DskipTests
146+
147+
# Build
148+
mvn clean package
149+
150+
```
151+
### .NET
152+
153+
```yaml
154+
- name: Build source code
155+
run: |
156+
# Install dependencies
157+
dotnet restore
158+
159+
# Build
160+
dotnet build
124161
125-
jobs:
126-
validate:
127-
runs-on: ubuntu-latest
128-
permissions:
129-
id-token: write # Required for OIDC authentication
130-
contents: read # Required to check out the repository
131-
steps:
132-
- uses: actions/checkout@v3
133-
134-
- name: Configure AWS credentials with OIDC
135-
uses: aws-actions/configure-aws-credentials@v2
136-
with:
137-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
138-
- name: Validate Lambda deployment (no changes)
139-
uses: aws-actions/amazon-lambda-deploy@v1
140-
with:
141-
function-name: my-lambda-function
142-
code-artifacts-dir: ./dist
143-
dry-run: true
144162
```
145163
146164
## Inputs
@@ -187,32 +205,17 @@ jobs:
187205

188206
This action relies on the [default behavior of the AWS SDK for JavaScript](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html) to determine AWS credentials and region. Use the [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) action to configure the GitHub Actions environment for AWS authentication.
189207

190-
### OpenID Connect (OIDC) - Recommended Approach
208+
### OpenID Connect (OIDC)
191209

192210
We **highly recommend** using OpenID Connect (OIDC) to authenticate with AWS. OIDC allows your GitHub Actions workflows to access AWS resources without storing AWS credentials as long-lived GitHub secrets.
193211

194212
Here's an example of using OIDC with the aws-actions/configure-aws-credentials action:
195213

196214
```yaml
197-
jobs:
198-
deploy:
199-
runs-on: ubuntu-latest
200-
permissions:
201-
id-token: write # Required for OIDC authentication
202-
contents: read # Required to check out the repository
203-
steps:
204-
- uses: actions/checkout@v3
205-
206215
- name: Configure AWS credentials with OIDC
207216
uses: aws-actions/configure-aws-credentials@v2
208217
with:
209218
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
210-
211-
- name: Deploy Lambda function
212-
uses: aws-actions/amazon-lambda-deploy@v1
213-
with:
214-
function-name: my-lambda-function
215-
code-artifacts-dir: ./dist
216219
```
217220

218221
To use OIDC authentication, you must configure a trust policy in AWS IAM that allows GitHub Actions to assume an IAM role. Here's an example trust policy:

__tests__ /code_artifacts.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { packageCodeArtifacts } = require('../index');
2+
3+
jest.mock('@actions/core');
4+
jest.mock('fs/promises');
5+
jest.mock('adm-zip');
6+
jest.mock('path');
7+
8+
describe('Code Artifacts Tests', () => {
9+
test('should throw error when artifactsDir is not provided', async () => {
10+
await expect(packageCodeArtifacts()).rejects.toThrow('Code artifacts directory path must be provided');
11+
});
12+
13+
test('should throw error when artifactsDir is null', async () => {
14+
await expect(packageCodeArtifacts(null)).rejects.toThrow('Code artifacts directory path must be provided');
15+
});
16+
17+
test('should throw error when artifactsDir is empty string', async () => {
18+
await expect(packageCodeArtifacts('')).rejects.toThrow('Code artifacts directory path must be provided');
19+
});
20+
});
File renamed without changes.

__tests__ /dry_run_mode.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const core = require('@actions/core');
2+
const { LambdaClient } = require('@aws-sdk/client-lambda');
3+
const mainModule = require('../index');
4+
const validations = require('../validations');
5+
6+
jest.mock('@actions/core');
7+
jest.mock('@aws-sdk/client-lambda');
8+
jest.mock('@aws-sdk/client-s3');
9+
jest.mock('@aws-sdk/client-sts');
10+
jest.mock('fs/promises');
11+
jest.mock('adm-zip');
12+
jest.mock('path');
13+
14+
describe('Dry Run Mode Tests', () => {
15+
beforeEach(() => {
16+
jest.clearAllMocks();
17+
process.env.AWS_REGION = 'us-east-1';
18+
});
19+
20+
test('should skip configuration updates in dry run mode when config changed', async () => {
21+
const mockClient = {
22+
send: jest.fn().mockResolvedValue({ Runtime: 'nodejs18.x', MemorySize: 256 })
23+
};
24+
LambdaClient.mockImplementation(() => mockClient);
25+
26+
validations.validateAllInputs = jest.fn().mockReturnValue({
27+
valid: true,
28+
functionName: 'test-function',
29+
parsedEnvironment: {},
30+
dryRun: true,
31+
role: 'arn:aws:iam::123456789012:role/lambda-role'
32+
});
33+
34+
jest.spyOn(mainModule, 'checkFunctionExists').mockResolvedValue(true);
35+
jest.spyOn(mainModule, 'packageCodeArtifacts').mockResolvedValue('/tmp/test.zip');
36+
jest.spyOn(mainModule, 'createFunction').mockResolvedValue();
37+
jest.spyOn(mainModule, 'hasConfigurationChanged').mockReturnValue(true);
38+
39+
await mainModule.run();
40+
});
41+
});

0 commit comments

Comments
 (0)