Skip to content

Commit

Permalink
infra: fix persist role names
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenkhavi committed Dec 22, 2023
1 parent 9a3a315 commit 21d3b35
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
- name: Install project dependencies
run: cd infra && yarn install
- name: Bootstrap CDK
if: ${{ vars.CDK_BOOTSTRAP == 'true' }}
working-directory: infra
run: |
yarn cdk bootstrap
Expand Down
3 changes: 2 additions & 1 deletion infra/stacks/iam-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AWS_ACCOUNT_ID,
AWS_REGION,
BaseStackProps,
getLambdaExecRoleName,
getResourceId,
} from '../utils';

Expand All @@ -24,7 +25,7 @@ export class IAMStack extends Stack {
getResourceId('LambdaFunctionExecutionRole', name),
{
assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
roleName: `${this.stackName}-${name}-lambda-function-execution`,
roleName: getLambdaExecRoleName(name),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName(
'service-role/AWSLambdaBasicExecutionRole'
Expand Down
4 changes: 2 additions & 2 deletions infra/stacks/lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AWS_ACCOUNT_ID,
AWS_REGION,
BaseStackProps,
getLambdaExecRoleName,
getResourceId,
} from '../utils';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
Expand All @@ -22,15 +23,14 @@ export class LambdaStack extends Stack {
new Function(this, getResourceId('LambdaFunction', name), {
runtime: Runtime.NODEJS_20_X,
code: Code.fromAsset(`../dist/apps/${name}`),
timeout: Duration.seconds(50), // ? https://stackoverflow.com/questions/36419442/the-role-defined-for-the-function-cannot-be-assumed-by-lambda
environment: {},
handler: 'main.handler',
role: Role.fromRoleName(
this,
getResourceId('IAM', name),
`${this.stackName}-${name}-lambda-function-execution`
),
functionName: `${this.stackName}-${name}`,
functionName: getLambdaExecRoleName(name),
});
});
}
Expand Down
4 changes: 4 additions & 0 deletions infra/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ const camalize = (str: string) => {
export const getResourceId = (resourceName: string, funcName: string) => {
return `${camalize(funcName)}${resourceName}`;
};

export const getLambdaExecRoleName = (funcName: string) => {
return `${ENVIRONMENT}-${funcName}-lambda-function-execution`;
};

0 comments on commit 21d3b35

Please sign in to comment.