-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the custom resource provider as proposed in #9
This change makes it such that each custom resource is split into 2 parts The "resource provider" and instances of the custom resource, that refer to a given provider. This also for the Lambda backed provider adds the ability to add custom permissions :)
- Loading branch information
1 parent
32ccb48
commit fb7641e
Showing
6 changed files
with
111 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,47 @@ | ||
import { Stack, Token } from "aws-cdk"; | ||
import { Construct, PolicyStatement, Token } from "aws-cdk"; | ||
import { Lambda, LambdaProps } from 'aws-cdk-lambda'; | ||
|
||
/** | ||
* Base class for Custom Resource providers, that details how the custom resource is created | ||
*/ | ||
export abstract class CustomResourceImplementation { | ||
export interface CustomResourceImplementation { | ||
/** | ||
* Return the provider ID for the provider in the given stack | ||
* | ||
* Returns either a Lambda ARN or an SNS topic ARN. | ||
*/ | ||
public abstract providerArn(stack: Stack): Token; | ||
providerArn(): Token; | ||
} | ||
|
||
/** | ||
* Properties to pass to a Lambda-backed custom resource provider | ||
*/ | ||
export interface LambdaBackedCustomResourceProps { | ||
/** | ||
* A unique identifier to identify this lambda | ||
* | ||
* The identifier should be unique across all custom resource providers. | ||
* We recommend generating a UUID per provider. | ||
*/ | ||
uuid: string; | ||
|
||
/** | ||
* Properties to instantiate the Lambda | ||
*/ | ||
lambdaProperties: LambdaProps; | ||
lambdaProperties: LambdaPropsWithPermissions; | ||
} | ||
|
||
export interface LambdaPropsWithPermissions extends LambdaProps { | ||
permissions?: PolicyStatement[]; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
/** | ||
* Custom Resource implementation that is backed by a Lambda function | ||
*/ | ||
export class LambdaBackedCustomResource extends CustomResourceImplementation { | ||
constructor(private readonly props: LambdaBackedCustomResourceProps) { | ||
super(); | ||
} | ||
export class LambdaBackedCustomResource implements CustomResourceImplementation { | ||
|
||
public providerArn(stack: Stack): Token { | ||
const providerLambda = this.ensureLambda(stack); | ||
return providerLambda.functionArn; | ||
} | ||
private readonly lambda: Lambda; | ||
|
||
/** | ||
* Add a fresh Lambda to the stack, or return the existing one if it already exists | ||
*/ | ||
private ensureLambda(stack: Stack): Lambda { | ||
const name = slugify(this.props.uuid); | ||
const existing = stack.tryFindChild(name); | ||
if (existing) { | ||
// Just assume this is true | ||
return existing as Lambda; | ||
constructor(parent: Construct, name: string, private readonly props: LambdaBackedCustomResourceProps) { | ||
this.lambda = new Lambda(parent, name, this.props.lambdaProperties); | ||
if (this.props.lambdaProperties.permissions && this.props.lambdaProperties.permissions.length > 0) { | ||
this.props.lambdaProperties.permissions.forEach(permission => this.lambda.addToRolePolicy(permission)); | ||
} | ||
|
||
const newFunction = new Lambda(stack, name, this.props.lambdaProperties); | ||
return newFunction; | ||
} | ||
} | ||
|
||
function slugify(x: string): string { | ||
return x.replace(/[^a-zA-Z0-9]/g, ''); | ||
public providerArn(): Token { | ||
return this.lambda.functionArn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,6 @@ export interface CustomResourceProps { | |
* The provider that is going to implement this custom resource | ||
*/ | ||
provider: CustomResourceImplementation; | ||
This comment has been minimized.
Sorry, something went wrong.
eladb
Contributor
|
||
|
||
/** | ||
* Properties to pass to the Lambda | ||
*/ | ||
properties?: Properties; | ||
} | ||
|
||
/** | ||
|
@@ -29,29 +24,51 @@ export interface CustomResourceProps { | |
* that hides the choice of provider, and accepts a strongly-typed properties | ||
* object with the properties your provider accepts. | ||
*/ | ||
export class CustomResource extends cloudformation.CustomResource { | ||
export class CustomResource extends Construct { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
// Needs to be implemented using inheritance because we must override the `renderProperties` | ||
This comment has been minimized.
Sorry, something went wrong. |
||
// The generated props classes will never render properties that they don't know about. | ||
private readonly stack: Stack; | ||
private readonly provider: CustomResourceImplementation; | ||
|
||
constructor(parent: Construct, name: string, props: CustomResourceProps) { | ||
super(parent, name); | ||
this.stack = Stack.find(parent); | ||
this.provider = props.provider; | ||
} | ||
|
||
/** | ||
* Add a new instance of the custom resource to the stack | ||
*/ | ||
public resourceInstance(name: string, properties?: Properties) { | ||
return new CustomResourceInstance(this, name, { | ||
stack: this.stack, | ||
provider: this.provider, | ||
userProperties: properties} | ||
); | ||
} | ||
} | ||
|
||
export interface CustomResourceInstanceProps { | ||
stack: Stack, | ||
provider: CustomResourceImplementation, | ||
userProperties?: Properties, | ||
} | ||
|
||
export class CustomResourceInstance extends cloudformation.CustomResource { | ||
|
||
private readonly userProperties?: Properties; | ||
|
||
constructor(parent: Construct, name: string, props: CustomResourceProps) { | ||
const stack = Stack.find(parent); | ||
constructor(parent: CustomResource, name: string, properties: CustomResourceInstanceProps) { | ||
super(parent, name, { | ||
serviceToken: props.provider.providerArn(stack), | ||
serviceToken: properties.provider.providerArn() | ||
}); | ||
|
||
this.userProperties = props.properties; | ||
this.userProperties = properties.userProperties; | ||
} | ||
|
||
/** | ||
* Override renderProperties to mix in the user-defined properties | ||
*/ | ||
protected renderProperties(): {[key: string]: any} { | ||
const props = super.renderProperties(); | ||
return Object.assign(props, uppercaseProperties(this.userProperties || {})); | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I can't find a reason not to add this to
LambdaProps
. Looks like it might be useful in the general case.