|
| 1 | +/** |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +// Imports |
| 15 | +import * as defaults from "@aws-solutions-constructs/core"; |
| 16 | +import * as lambda from "@aws-cdk/aws-lambda"; |
| 17 | +import * as secretsmanager from "@aws-cdk/aws-secretsmanager"; |
| 18 | +import * as ec2 from "@aws-cdk/aws-ec2"; |
| 19 | +import { Construct } from "@aws-cdk/core"; |
| 20 | + |
| 21 | +/** |
| 22 | + * @summary The properties for the LambdaToSecretsmanager class. |
| 23 | + */ |
| 24 | +export interface LambdaToSecretsmanagerProps { |
| 25 | + /** |
| 26 | + * Existing instance of Lambda Function object, if this is set then the lambdaFunctionProps is ignored. |
| 27 | + * |
| 28 | + * @default - None |
| 29 | + */ |
| 30 | + readonly existingLambdaObj?: lambda.Function; |
| 31 | + /** |
| 32 | + * User provided props to override the default props for the Lambda function. |
| 33 | + * |
| 34 | + * @default - Default properties are used. |
| 35 | + */ |
| 36 | + readonly lambdaFunctionProps?: lambda.FunctionProps; |
| 37 | + /** |
| 38 | + * Existing instance of Secret object, if this is set then secretProps is ignored. |
| 39 | + * |
| 40 | + * @default - Default props are used |
| 41 | + */ |
| 42 | + readonly existingSecretObj?: secretsmanager.Secret; |
| 43 | + /** |
| 44 | + * Optional user-provided props to override the default props for the Secret. |
| 45 | + * |
| 46 | + * @default - Default props are used |
| 47 | + */ |
| 48 | + readonly secretProps?: secretsmanager.SecretProps; |
| 49 | + /** |
| 50 | + * An existing VPC for the construct to use (construct will NOT create a new VPC in this case) |
| 51 | + */ |
| 52 | + readonly existingVpc?: ec2.IVpc; |
| 53 | + /** |
| 54 | + * Properties to override default properties if deployVpc is true |
| 55 | + */ |
| 56 | + readonly vpcProps?: ec2.VpcProps; |
| 57 | + /** |
| 58 | + * Whether to deploy a new VPC |
| 59 | + * |
| 60 | + * @default - false |
| 61 | + */ |
| 62 | + readonly deployVpc?: boolean; |
| 63 | + /** |
| 64 | + * Optional Name for the Secret environment variable set for the Lambda function. |
| 65 | + * |
| 66 | + * @default - SECRET_NAME |
| 67 | + */ |
| 68 | + readonly secretEnvironmentVariableName?: string; |
| 69 | + /** |
| 70 | + * Optional secret permissions to grant to the Lambda function. |
| 71 | + * One of the following may be specified: "Read" or "ReadWrite". |
| 72 | + * |
| 73 | + * @default - Read only acess is given to the Lambda function if no value is specified. |
| 74 | + */ |
| 75 | + readonly grantWriteAccess?: string; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * @summary The LambdaToSecretsmanager class. |
| 80 | + */ |
| 81 | +export class LambdaToSecretsmanager extends Construct { |
| 82 | + public readonly lambdaFunction: lambda.Function; |
| 83 | + public readonly secret: secretsmanager.Secret; |
| 84 | + public readonly vpc?: ec2.IVpc; |
| 85 | + |
| 86 | + /** |
| 87 | + * @summary Constructs a new instance of the LambdaToSecretsmanager class. |
| 88 | + * @param {cdk.App} scope - represents the scope for all the resources. |
| 89 | + * @param {string} id - this is a a scope-unique id. |
| 90 | + * @param {LambdaToSecretsmanagerProps} props - user provided props for the construct. |
| 91 | + * @access public |
| 92 | + */ |
| 93 | + constructor(scope: Construct, id: string, props: LambdaToSecretsmanagerProps) { |
| 94 | + super(scope, id); |
| 95 | + |
| 96 | + if (props.deployVpc || props.existingVpc) { |
| 97 | + if (props.deployVpc && props.existingVpc) { |
| 98 | + throw new Error("More than 1 VPC specified in the properties"); |
| 99 | + } |
| 100 | + |
| 101 | + this.vpc = defaults.buildVpc(scope, { |
| 102 | + defaultVpcProps: defaults.DefaultIsolatedVpcProps(), |
| 103 | + existingVpc: props.existingVpc, |
| 104 | + userVpcProps: props.vpcProps, |
| 105 | + constructVpcProps: { |
| 106 | + enableDnsHostnames: true, |
| 107 | + enableDnsSupport: true, |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.SECRETS_MANAGER); |
| 112 | + } |
| 113 | + |
| 114 | + // Setup the Lambda function |
| 115 | + this.lambdaFunction = defaults.buildLambdaFunction(this, { |
| 116 | + existingLambdaObj: props.existingLambdaObj, |
| 117 | + lambdaFunctionProps: props.lambdaFunctionProps, |
| 118 | + vpc: this.vpc, |
| 119 | + }); |
| 120 | + |
| 121 | + // Setup the Secret |
| 122 | + if (props.existingSecretObj) { |
| 123 | + this.secret = props.existingSecretObj; |
| 124 | + } else { |
| 125 | + this.secret = defaults.buildSecretsManagerSecret(this, 'secret', props.secretProps); |
| 126 | + } |
| 127 | + |
| 128 | + // Configure environment variables |
| 129 | + const secretEnvironmentVariableName = props.secretEnvironmentVariableName || 'SECRET_ARN'; |
| 130 | + this.lambdaFunction.addEnvironment(secretEnvironmentVariableName, this.secret.secretArn); |
| 131 | + |
| 132 | + // Enable read permissions for the Lambda function by default |
| 133 | + this.secret.grantRead(this.lambdaFunction); |
| 134 | + |
| 135 | + if (props.grantWriteAccess === 'ReadWrite') { |
| 136 | + this.secret.grantWrite(this.lambdaFunction); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments