async function handleEvent()

in lib/custom-resource-handlers/src/pgp-secret.ts [29:70]


async function handleEvent(event: cfn.Event, context: lambda.Context): Promise<cfn.ResourceAttributes> {
  const props = event.ResourceProperties;

  if (event.RequestType !== cfn.RequestType.DELETE) {
    cfn.validateProperties(props, {
      Description: false,
      Email: true,
      Expiry: true,
      Identity: true,
      KeyArn: false,
      KeySizeBits: true,
      SecretName: true,
      Version: false,
      DeleteImmediately: false,
    });
  }

  let newKey = event.RequestType === cfn.RequestType.CREATE;

  if (event.RequestType === cfn.RequestType.UPDATE) {
    const oldProps = event.OldResourceProperties;
    const immutableFields = ['Email', 'Expiry', 'Identity', 'KeySizeBits', 'SecretName', 'Version'];
    for (const key of immutableFields) {
      if (props[key] !== oldProps[key]) {
        // eslint-disable-next-line no-console
        console.log(`New key required: ${key} changed from ${oldProps[key]} to ${props[key]}`);
        newKey = true;
      }
    }
  }

  switch (event.RequestType) {
    case cfn.RequestType.CREATE:
    case cfn.RequestType.UPDATE:
    // If we're UPDATE and get a new key, we'll issue a new Physical ID.
      return newKey
        ? _createNewKey(event, context)
        : _updateExistingKey(event as cfn.UpdateEvent, context);
    case cfn.RequestType.DELETE:
      return _deleteSecret(event);
  }
}