amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Auth Override: Link a Custom Resource to an Override of the Auth Resource

Open Stefan-WW opened this issue 3 years ago • 7 comments

How did you install the Amplify CLI?

npm install -g @aws-amplify/cli

If applicable, what version of Node.js are you using?

v14.19.3

Amplify CLI Version

10.6.2

What operating system are you using?

Windows

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

Describe the bug

When you do an addCfnParameter in an Override for an Auth Resource, this method doesn't support the reference to other resources.

In my case i have an custom resource (KMS Key) which has the ARN as an output Parameter. In the override of the auth resource i have to get this arn.

I tried it in the following way: image

But the method addCfnParameter doesn't support the third parameter. Without this parameter i'm not able to reference to to the KMS Key ARN right?

When i have an Override for an API Resource, i also can use addCfnParameter . But in the api Override the third parameter is supported.

Here you can see the difference image

Why is an override of an auth resource more restricted as for an api resource?

Are there other possibilities to solve my problem?

Expected behavior

Overrides should have the same capabilities.

Reproduction steps

  1. amplify override auth
  2. amplify override api
  3. see the different interfaces for the resources.addCfnParameter

Project Identifier

No response

Log output

# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [X] I have removed any sensitive information from my code snippets and submission.

Stefan-WW avatar Jan 22 '23 22:01 Stefan-WW

Hey @StefanVolkmer-WW, thank you for reaching out. The team is looking into improving the experience on override. As a temporary workaround, we could replace the imported type with a less restrictive types and add the Fn::GetAtt . Refer to https://github.com/aws-amplify/amplify-cli/issues/9260#issuecomment-993112251 providing an example.

ykethan avatar Jan 23 '23 21:01 ykethan

Hi @ykethan Cool it would be awesome if you could add the Fn::GetAtt.

But only to double check, i think if you replacing/extending the type in the "type.ts" file, its not enough. It would be also necassary to change the implementation which is called?

I debugged into the addCfnParameter. And if it's an override auth, then other code seems to be executed. And here there is also no third parameter/support of Fn::GetAtt

image

Kind Regards Stefan

Stefan-WW avatar Jan 23 '23 22:01 Stefan-WW

Note: the type issue should be resolved with https://github.com/aws-amplify/amplify-cli/pull/9936

josefaidt avatar Jan 24 '23 21:01 josefaidt

Hey @StefanVolkmer-WW :wave: we were able to reference an output from a custom resource, but with a little manual lifting. First, we create the custom resource for the KMS key:

import * as cdk from '@aws-cdk/core'
import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper'
import { AmplifyDependentResourcesAttributes } from '../../types/amplify-dependent-resources-ref'
import * as kms from '@aws-cdk/aws-kms'

export class cdkStack extends cdk.Stack {
  constructor(
    scope: cdk.Construct,
    id: string,
    props?: cdk.StackProps,
    amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps
  ) {
    super(scope, id, props)

    new cdk.CfnParameter(this, 'env', {
      type: 'String',
      description: 'Current Amplify CLI env name',
    })

    const key = new kms.Key(this, 'MyKmsKey', {
      enableKeyRotation: true,
    })

    new cdk.CfnOutput(this, 'KmsKeyId', {
      value: key.keyId,
    })
    new cdk.CfnOutput(this, 'KmsKeyArn', {
      value: key.keyArn,
    })
  }
}

then in amplify/backend/backend-config.json we will want to add this custom resource as a dependency for auth. Below is a snippet of that file

  "auth": {
    "117915b9b00b5": {
      "customAuth": false,
      "dependsOn": [
        {
          "attributes": [
            "KmsKeyArn"
          ],
          "category": "custom",
          "resourceName": "customResourcefdcb8925"
        }
      ],

From here, run amplify env checkout <current-env-name> to ensure the dependency is set, which can be verified by inspecting the auth resource in the root stack at amplify/backend/awscloudformation/build/root-cloudformation-stack.json. Then move to the auth override to create the KmsKeyArn parameter to allow the CLI to feed the value from the custom resource:

import { AmplifyAuthCognitoStackTemplate } from '@aws-amplify/cli-extensibility-helper'

export function override(resources: AmplifyAuthCognitoStackTemplate) {
  // this is generated by Amplify CLI after creating the dependency in backend-config.json
  const parameter = 'customcustomResourcefdcb8925KmsKeyArn'
  resources.addCfnParameter(
    {
      type: 'String',
      description: 'my kms key arn',
      default: 'NONE',
    },
    parameter
  )

  resources.userPool.addPropertyOverride('LambdaConfig', {
    ...resources.userPool.lambdaConfig,
    KMSKeyID: {
      Ref: parameter,
    },
  })
}

The parameter name is constructed as customcustomResourcefdcb8925KmsKeyArn which follows <category><resource-name><parameter-name>

Let us know if this works for you!

josefaidt avatar Feb 01 '23 16:02 josefaidt

Closing due to inactivity

josefaidt avatar Feb 16 '23 01:02 josefaidt

@josefaidt When running the amplify env checkout <current-env-name> step from your Feb 1st response above, I'm not seeing the dependency get set for the auth resource in the amplify/backend/awscloudformation/build/root-cloudformation-stack.json file. Nor do I see it there after amplify auth build or amplify build. Then, in my auth overrides file, the default value of "NONE" is being used for the Key Arn parameter. Do you have any ideas as to why the dependency might not be propagating to the root CF stack?

I'm using the latest amplify CLI (v12.8.2)

kennyd-reacthealth avatar Dec 08 '23 05:12 kennyd-reacthealth

Apparently Order of the dependencies in backend-config.json matters. When my Custom stack (KMS key) was first in the list of dependencies, it was never able to successfully deploy. When it's last in the list, it seems to work.

kennyd-reacthealth avatar Dec 08 '23 05:12 kennyd-reacthealth