aws-lambda#CloudFormationCustomResourceUpdateEvent TypeScript Examples
The following examples show how to use
aws-lambda#CloudFormationCustomResourceUpdateEvent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: codecommit.ts From aws-codecommit-devops-model with MIT No Attribution | 5 votes |
approvalRuleTemplate: CloudFormationCustomResourceHandler = async (event, _context) => {
console.info(`Receiving ApprovalRuleEvent of CodeCommit ${JSON.stringify(event, null, 2)}`);
var responseData: any;
var result = SUCCESS;
var reason: any = '';
var resourceId: string | undefined = undefined;
try {
switch (event.RequestType) {
case 'Create':
const createTempalteResponse = await codecommit.createApprovalRuleTemplate({
approvalRuleTemplateName: event.ResourceProperties.ApprovalRuleTemplateName,
approvalRuleTemplateDescription: event.ResourceProperties.ApprovalRuleTemplateDescription || '',
approvalRuleTemplateContent: buildTemplateContent(event.ResourceProperties),
}).promise();
console.info(`Created approval rule template ${JSON.stringify(createTempalteResponse.$response.data, null, 2)}.`);
responseData = (createTempalteResponse.$response.data as CodeCommit.Types.CreateApprovalRuleTemplateOutput).approvalRuleTemplate;
resourceId = responseData.approvalRuleTemplateId;
break;
case 'Update':
const updateEvent = event as CloudFormationCustomResourceUpdateEvent;
resourceId = updateEvent.PhysicalResourceId;
const changes = [];
if (!equal(updateEvent.ResourceProperties.Template, updateEvent.OldResourceProperties.Template)) {
changes.push(codecommit.updateApprovalRuleTemplateContent({
approvalRuleTemplateName: updateEvent.OldResourceProperties.ApprovalRuleTemplateName,
newRuleContent: buildTemplateContent(updateEvent.ResourceProperties),
}).promise())
}
if (updateEvent.ResourceProperties.ApprovalRuleTemplateDescription !== updateEvent.OldResourceProperties.ApprovalRuleTemplateDescription) {
changes.push(codecommit.updateApprovalRuleTemplateDescription({
approvalRuleTemplateName: updateEvent.OldResourceProperties.ApprovalRuleTemplateName,
approvalRuleTemplateDescription: updateEvent.ResourceProperties.ApprovalRuleTemplateDescription || '',
}).promise());
}
if (changes.length > 0) {
const updateDescAndTemplateRt = await Promise.all(changes);
console.info(`Updated approval rule '${updateEvent.OldResourceProperties.ApprovalRuleTemplateName}' descirption and template content ${updateDescAndTemplateRt}.`);
responseData = (updateDescAndTemplateRt[0].$response.data as CodeCommit.Types.UpdateApprovalRuleTemplateContentOutput |
CodeCommit.Types.UpdateApprovalRuleTemplateDescriptionOutput).approvalRuleTemplate;
}
if (updateEvent.ResourceProperties.ApprovalRuleTemplateName !== updateEvent.OldResourceProperties.ApprovalRuleTemplateName) {
const updatedApprovalTemplate = await codecommit.updateApprovalRuleTemplateName({
newApprovalRuleTemplateName: updateEvent.ResourceProperties.ApprovalRuleTemplateName,
oldApprovalRuleTemplateName: updateEvent.OldResourceProperties.ApprovalRuleTemplateName,
}).promise();
console.log(`Updated approval rule name from '${updateEvent.OldResourceProperties.ApprovalRuleTemplateName} to '${updateEvent.ResourceProperties.ApprovalRuleTemplateName}'.`)
responseData = (updatedApprovalTemplate as CodeCommit.Types.UpdateApprovalRuleTemplateNameOutput).approvalRuleTemplate;
}
break;
case 'Delete':
const deleteEvent = event as CloudFormationCustomResourceDeleteEvent;
resourceId = deleteEvent.PhysicalResourceId;
const deleteTempalteResponse = await codecommit.deleteApprovalRuleTemplate({
approvalRuleTemplateName: event.ResourceProperties.ApprovalRuleTemplateName,
}).promise();
console.info(`Deleted approval rule template ${JSON.stringify(deleteTempalteResponse.$response.data, null, 2)}.`);
responseData = deleteTempalteResponse.$response.data;
break;
}
} catch (err) {
if (err instanceof Error) {
console.error(`Failed to create approval rule template due to ${err}.`);
responseData = err.message;
result = FAILED;
reason = err.message;
}
}
return await sendResponse({ Status: result, Reason: reason, PhysicalResourceId: (resourceId ? resourceId : _context.logStreamName), Data: responseData }, event);
}
Example #2
Source File: codecommit.ts From aws-codecommit-devops-model with MIT No Attribution | 5 votes |
approvalRuleRepoAssociation: CloudFormationCustomResourceHandler = async (event, _context) => {
console.info(`Receiving ApprovalRuleAssociationEvent of CodeCommit ${JSON.stringify(event, null, 2)}`);
var responseData: any;
var result = SUCCESS;
var reason: any = '';
var resourceId: string | undefined = undefined;
try {
switch (event.RequestType) {
case 'Create':
const assoRsp = await codecommit.batchAssociateApprovalRuleTemplateWithRepositories({
approvalRuleTemplateName: event.ResourceProperties.ApprovalRuleTemplateName,
repositoryNames: event.ResourceProperties.RepositoryNames
}).promise();
console.info(`Associated ${assoRsp.associatedRepositoryNames} with ${event.ResourceProperties.ApprovalRuleTemplateName}.`);
resourceId = `${event.ResourceProperties.ApprovalRuleTemplateName}-repos`;
responseData = {
AssociatedRepoNames: assoRsp.associatedRepositoryNames,
};
break;
case 'Update':
const updateEvent = event as CloudFormationCustomResourceUpdateEvent;
resourceId = `${updateEvent.ResourceProperties.ApprovalRuleTemplateName}-repos`;
const added = updateEvent.ResourceProperties.ApprovalRuleTemplateName ==
updateEvent.OldResourceProperties.ApprovalRuleTemplateName ?
(updateEvent.ResourceProperties.RepositoryNames as Array<string>).filter((element, index, array) => {
return !(updateEvent.OldResourceProperties.RepositoryNames as Array<string>).includes(element);
}) : updateEvent.ResourceProperties.RepositoryNames as Array<string>;
const deleted = updateEvent.ResourceProperties.ApprovalRuleTemplateName ==
updateEvent.OldResourceProperties.ApprovalRuleTemplateName ?
(updateEvent.OldResourceProperties.RepositoryNames as Array<string>).filter((element, index, array) => {
return !(updateEvent.ResourceProperties.RepositoryNames as Array<string>).includes(element);
}) : updateEvent.OldResourceProperties.RepositoryNames as Array<string>;
const changes = [];
if (added.length > 0) {
const assoRsp = await codecommit.batchAssociateApprovalRuleTemplateWithRepositories({
approvalRuleTemplateName: updateEvent.ResourceProperties.ApprovalRuleTemplateName,
repositoryNames: added
}).promise();
console.info(`Associated ${assoRsp.associatedRepositoryNames} with ${updateEvent.ResourceProperties.ApprovalRuleTemplateName}.`);
responseData = {
AssociatedRepoNames: assoRsp.associatedRepositoryNames,
};
}
if (deleted.length > 0) {
const disassoRsp = codecommit.batchDisassociateApprovalRuleTemplateFromRepositories({
approvalRuleTemplateName: updateEvent.OldResourceProperties.ApprovalRuleTemplateName,
repositoryNames: deleted,
}).promise();
console.info(`DisAssociated ${(await disassoRsp).disassociatedRepositoryNames} with ${updateEvent.OldResourceProperties.ApprovalRuleTemplateName}.`);
responseData = Object.assign(responseData ? responseData : {}, {
DisAssociatedRepoNames: (await disassoRsp).disassociatedRepositoryNames,
});
}
break;
case 'Delete':
const deleteEvent = event as CloudFormationCustomResourceDeleteEvent;
resourceId = deleteEvent.PhysicalResourceId;
const disassociateRsp = await codecommit.batchDisassociateApprovalRuleTemplateFromRepositories({
approvalRuleTemplateName: deleteEvent.ResourceProperties.ApprovalRuleTemplateName,
repositoryNames: deleteEvent.ResourceProperties.RepositoryNames
}).promise();
console.info(`Disassociated ${disassociateRsp.disassociatedRepositoryNames} with ${deleteEvent.ResourceProperties.ApprovalRuleTemplateName}.`);
responseData = {
DisAssociatedRepoNames: disassociateRsp.disassociatedRepositoryNames,
};
break;
}
} catch (err) {
if (err instanceof Error) {
console.error(`Failed to associate/disassociate approval rule template with repos due to ${err}.`);
responseData = err.message;
result = FAILED;
reason = err.message;
}
}
return await sendResponse({ Status: result, Reason: reason, PhysicalResourceId: (resourceId ? resourceId : _context.logStreamName), Data: responseData }, event);
}
Example #3
Source File: update.ts From aws-cdk-ses-domain-identity with MIT License | 5 votes |
export class UpdateCustomResourceHandler extends CustomResourceHandler<CloudFormationCustomResourceUpdateEvent> {
public async consumeEvent() {
const dnsChanged = this.hasChanged("DomainName") || this.hasChanged("HostedZoneId");
const dkimChanged = this.hasChanged("DKIM");
// If DNS is changed, Verify changed domain first and then revoke old one
if (dnsChanged) {
const newVerifier = Verifier.from(this.event.ResourceProperties);
const oldVerifier = Verifier.from(this.event.OldResourceProperties);
await newVerifier.verifyIdentity(true);
if (this.event.ResourceProperties.DKIM) {
await newVerifier.enableDKIM(true);
}
if (this.event.OldResourceProperties.DKIM) {
await oldVerifier.disableDKIM();
}
await oldVerifier.revokeIdentity();
return {
// Update Physical Resource ID
physicalResourceId: newVerifier.domainName,
data: {},
};
}
if (dkimChanged) {
const verifier = Verifier.from(this.event.ResourceProperties);
if (this.event.ResourceProperties.DKIM) {
await verifier.enableDKIM(true);
} else {
await verifier.disableDKIM();
}
}
return {
// Keep Physical Resource ID
physicalResourceId: this.event.PhysicalResourceId,
data: {},
};
}
private hasChanged(name: string): boolean {
return this.event.OldResourceProperties[name] !== this.event.ResourceProperties[name];
}
}