Skip to content

Commit 735232d

Browse files
mprzytulskimadelinevdp
authored andcommitted
allow to specify lambdaRoleName
1 parent bf48946 commit 735232d

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Configure the AWS KMS key id and lambdaArn for the plugin in serverless.yml:
3939

4040
- kmsKeyId: the `KeyId`, `Alias`, or `Arn` used to identify the KMS key
4141
(**Required**)
42+
- lambdaRoleName: the name of the lambda role you wish to grant access to KMS key.
43+
(Optional). If name is not specified the plugin will try with default role name.
4244
- lambdaRoleArn: the Arn of the lambda you wish to grant access to the KMS key
4345
(Optional). If an arn is not specified, the plugin will look for the default
4446
lambdaRole and obtain its arn. The default serverless lambda role follows the

index.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class ServerlessKmsGrants {
4040
if (grantID === null) {
4141
this.serverless.cli.log("Creating KMS grant for " + lambdaArn);
4242
await this.kms
43-
.createGrant({
44-
KeyId: keyArn,
45-
GranteePrincipal: lambdaArn,
46-
Operations: ["Encrypt", "Decrypt"],
47-
})
48-
.promise();
43+
.createGrant({
44+
KeyId: keyArn,
45+
GranteePrincipal: lambdaArn,
46+
Operations: ["Encrypt", "Decrypt"],
47+
})
48+
.promise();
4949
} else {
5050
this.serverless.cli.log("KMS grant already exists for " + lambdaArn);
5151
}
@@ -81,8 +81,8 @@ class ServerlessKmsGrants {
8181
return lambdaRole;
8282
}
8383

84-
async getLambdaArn() {
85-
let lambdaRole = this.getLambdaRole();
84+
async getLambdaArn(lambdaRoleName) {
85+
let lambdaRole = lambdaRoleName || this.getLambdaRole();
8686
const iam = new aws.IAM({
8787
region: this.serverless.service.provider.region,
8888
});
@@ -99,21 +99,31 @@ class ServerlessKmsGrants {
9999
}
100100

101101
let lambdaArn = _.get(
102-
this.serverless.service,
103-
"custom.kmsGrants.lambdaRoleArn",
102+
this.serverless.service,
103+
"custom.kmsGrants.lambdaRoleArn",
104+
);
105+
106+
let lambdaRoleName = _.get(
107+
this.serverless.service,
108+
"custom.kmsGrants.lambdaRoleName",
104109
);
110+
111+
if (lambdaRoleName) {
112+
lambdaArn = await this.getLambdaArn(lambdaRoleName);
113+
}
114+
105115
if (!lambdaArn) {
106116
this.serverless.cli.log(
107-
"'lambdaRoleArn' not defined, using default format for role name: <service>-<stage>-<region>-lambdaRole",
117+
"Neither 'lambdaRoleArn' or 'lambdaRoleName' not defined, using default format for role name: <service>-<stage>-<region>-lambdaRole",
108118
);
109119
lambdaArn = await this.getLambdaArn();
110120
}
111121

112122
const keyData = await this.kms.describeKey({ KeyId: keyId }).promise();
113123
const keyArn = keyData.KeyMetadata.Arn;
114124
const { Grants: grantsArray } = await this.kms
115-
.listGrants({ KeyId: keyArn })
116-
.promise();
125+
.listGrants({ KeyId: keyArn })
126+
.promise();
117127

118128
for (let i = 0; i < grantsArray.length; i++) {
119129
if (grantsArray[i].GranteePrincipal === lambdaArn) {

0 commit comments

Comments
 (0)