|
| 1 | +import { AttachedPolicy, CreateRoleResponse, GetRoleResponse, IAM } from "@aws-sdk/client-iam"; |
| 2 | +import { |
| 3 | + GetFunctionConfigurationCommandOutput, |
| 4 | + Lambda, |
| 5 | + Runtime, |
| 6 | + waitUntilFunctionActiveV2, |
| 7 | + waitUntilFunctionUpdated, |
| 8 | +} from "@aws-sdk/client-lambda"; |
| 9 | +import fs from "fs"; |
| 10 | +export const FunctionName = "aws-sdk-js-v3-e2e-echo"; |
| 11 | +export const Handler = "index.handler"; |
| 12 | +const LAMBDA_ROLE_NAME = "aws-sdk-js-v3-e2e-LambdaRole"; |
| 13 | + |
| 14 | +export async function setup() { |
| 15 | + const lambda = new Lambda({ |
| 16 | + region: "us-west-2", |
| 17 | + }); |
| 18 | + const getFn: null | GetFunctionConfigurationCommandOutput = await lambda |
| 19 | + .getFunctionConfiguration({ |
| 20 | + FunctionName, |
| 21 | + }) |
| 22 | + .catch(() => null); |
| 23 | + |
| 24 | + if (getFn) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const iam = new IAM({ |
| 29 | + region: "us-west-2", |
| 30 | + }); |
| 31 | + |
| 32 | + const roleName = LAMBDA_ROLE_NAME; |
| 33 | + const role: null | GetRoleResponse | CreateRoleResponse = await iam |
| 34 | + .getRole({ |
| 35 | + RoleName: roleName, |
| 36 | + }) |
| 37 | + .catch(() => null); |
| 38 | + |
| 39 | + if (!role) { |
| 40 | + console.info("Creating role", roleName); |
| 41 | + await iam.createRole({ |
| 42 | + RoleName: roleName, |
| 43 | + Path: "/", |
| 44 | + Description: "aws sdk js v3 lambda test role", |
| 45 | + AssumeRolePolicyDocument: JSON.stringify({ |
| 46 | + Version: "2012-10-17", |
| 47 | + Statement: [ |
| 48 | + { |
| 49 | + Effect: "Allow", |
| 50 | + Action: ["sts:AssumeRole"], |
| 51 | + Principal: { |
| 52 | + Service: ["lambda.amazonaws.com"], |
| 53 | + }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }), |
| 57 | + }); |
| 58 | + } else { |
| 59 | + console.info("Role exists", roleName); |
| 60 | + } |
| 61 | + |
| 62 | + const listAttachedRolePolicies = await iam.listAttachedRolePolicies({ |
| 63 | + RoleName: roleName, |
| 64 | + }); |
| 65 | + const policies = listAttachedRolePolicies.AttachedPolicies || []; |
| 66 | + |
| 67 | + const existingPolicies = policies.reduce((acc: Record<string, boolean>, cur: AttachedPolicy) => { |
| 68 | + if (cur.PolicyName) { |
| 69 | + acc[cur.PolicyName] = true; |
| 70 | + } |
| 71 | + return acc; |
| 72 | + }, {} as Record<string, boolean>); |
| 73 | + |
| 74 | + const required = ["AWSLambda_FullAccess"]; |
| 75 | + |
| 76 | + for (const requiredPolicy of required) { |
| 77 | + if (!existingPolicies[requiredPolicy]) { |
| 78 | + console.info("Attaching policy to role", requiredPolicy, roleName); |
| 79 | + await iam.attachRolePolicy({ |
| 80 | + RoleName: roleName, |
| 81 | + PolicyArn: `arn:aws:iam::aws:policy/${requiredPolicy}`, |
| 82 | + }); |
| 83 | + } else { |
| 84 | + console.info("Policy exists on role", requiredPolicy, roleName); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const getRole: null | GetRoleResponse = await iam |
| 89 | + .getRole({ |
| 90 | + RoleName: roleName, |
| 91 | + }) |
| 92 | + .catch(() => null); |
| 93 | + if (!getRole) { |
| 94 | + throw new Error("Role not found."); |
| 95 | + } else { |
| 96 | + console.info("Role found", roleName); |
| 97 | + } |
| 98 | + |
| 99 | + const roleArn = getRole.Role!.Arn!; |
| 100 | + |
| 101 | + if (getFn) { |
| 102 | + console.info("Function exists:", FunctionName); |
| 103 | + |
| 104 | + if ((getFn.Timeout ?? 0) < 5 * 60 || getFn?.Handler !== Handler) { |
| 105 | + await lambda.updateFunctionConfiguration({ |
| 106 | + FunctionName, |
| 107 | + Handler, |
| 108 | + Timeout: 5 * 60, |
| 109 | + }); |
| 110 | + await waitUntilFunctionUpdated( |
| 111 | + { |
| 112 | + client: lambda, |
| 113 | + maxWaitTime: 40, |
| 114 | + }, |
| 115 | + { |
| 116 | + FunctionName, |
| 117 | + } |
| 118 | + ); |
| 119 | + } |
| 120 | + // await lambda.updateFunctionCode({ |
| 121 | + // FunctionName, |
| 122 | + // ZipFile: fs.readFileSync(require.resolve("./function.zip")), |
| 123 | + // }); |
| 124 | + // console.info("Function code/configuration updated:", FunctionName); |
| 125 | + } else { |
| 126 | + await lambda.createFunction({ |
| 127 | + FunctionName, |
| 128 | + Role: roleArn, |
| 129 | + Code: { |
| 130 | + ZipFile: fs.readFileSync(require.resolve("./function.zip")), |
| 131 | + }, |
| 132 | + Runtime: Runtime.nodejs16x, |
| 133 | + Description: `aws sdk js v3 e2e test echo`, |
| 134 | + Timeout: 300, |
| 135 | + Handler, |
| 136 | + }); |
| 137 | + console.info("Function created:", FunctionName); |
| 138 | + } |
| 139 | + |
| 140 | + await waitUntilFunctionActiveV2( |
| 141 | + { |
| 142 | + maxWaitTime: 40, |
| 143 | + client: lambda, |
| 144 | + }, |
| 145 | + { |
| 146 | + FunctionName, |
| 147 | + } |
| 148 | + ); |
| 149 | +} |
0 commit comments