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

Custom Resources + Serverless Containers Don't Work

Open amirhajimirsadeghi opened this issue 5 years ago • 11 comments

Before opening, please confirm:

I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists. I have searched for duplicate or closed issues. I have read the guide for submitting bug reports. I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

npm i -g @aws-amplify/cli

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

v14.7.0

Amplify CLI Version

4.45.2

What operating system are you using?

Mac

Amplify Categories

api, Not applicable

Amplify Commands

push

Describe the bug

Referencing Custom Resources in Serverless Containers get overwritten during amplify push

I followed this guide to create a custom resource and reference it in the cloudformation for my serverless container but every time I add new things and run amplify push the file gets overwritten with my changes no longer there. This makes it impossible to reference custom references in serverless containers

Expected behavior

Adding custom resource references to Serverless Container using this guide should work and not overwrite the cloudformation file.

Reproduction steps

  1. enable and create a serverless container using this guide: https://docs.amplify.aws/cli/usage/containers#getting-started
  2. create a custom resource using this guide: https://docs.amplify.aws/cli/usage/customcf#n2-modify-folder-structure-for-custom-resource
  3. referencing the custom resource, give serverless container permission to access custom resource by following this guide: https://docs.amplify.aws/cli/usage/customcf#n2-modify-folder-structure-for-custom-resource
  4. run amplify push
  5. see that references to custom resource have been over written in serverless container cloudformation

GraphQL schema(s)

N/A

Log output

# Put your logs below this line


# Put schemas below this line


Additional information

No response

amirhajimirsadeghi avatar Apr 10 '21 05:04 amirhajimirsadeghi

I see the same issue. No idea how the team can release this feature with such a major bug

fr3shn3ss avatar Apr 20 '21 13:04 fr3shn3ss

@amirhajimirsadeghi could this be related? https://github.com/aws-amplify/amplify-cli/issues/7137

mattfysh avatar May 07 '21 07:05 mattfysh

No, @mattfysh this is related to https://github.com/aws-amplify/amplify-cli/issues/6918 I think I double created the ticket

amirhajimirsadeghi avatar May 07 '21 13:05 amirhajimirsadeghi

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 11 '21 01:06 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 13 '21 01:07 stale[bot]

Hey @amirhajimirsadeghi :wave: apologies for the delay! To clarify are you looking to use outputs from your custom resource in the category resource? Currently we support taking in outputs from categories into a custom category.

josefaidt avatar Jul 15 '21 20:07 josefaidt

@josefaidt I'm looking to use outputs from a custom resource in a serverless container API resource. However, in attempting to do so, the cloud formation gets overwritten and thus nothing can be used.

Importing custom resource outputs to other resources works just fine

amirhajimirsadeghi avatar Jul 16 '21 18:07 amirhajimirsadeghi

Hey @amirhajimirsadeghi thank you for the information! I will mark this as a feature request for the team to review

josefaidt avatar Jul 19 '21 18:07 josefaidt

Hi @amirhajimirsadeghi - we've just published an RFC on providing containers access permissions to external resources. Would love to get your feedback on it: https://github.com/aws-amplify/amplify-cli/issues/7792

renebrandel avatar Jul 23 '21 22:07 renebrandel

This is still a valid feature request. After creating a container-based API in our project and attempt to edit the CloudFormation template the edits are overwritten on amplify push

josefaidt avatar Feb 03 '23 23:02 josefaidt

There are some small workarounds for this. You can't prevent amplify push from overwriting the template, but you can trick it to generate the stack you want, to some extent.

I've found that adding to both backend-config.json and amplify-meta.json files some data manually (you need to duplicate it in both), and then running amplify push --force, makes several changes to the container stack. Specifically:

Add output parameters from another stack as input parameters

You need to update the dependsOn array and add the input parameters you want in the following format:

{
  "category": "",
  "resourceName": "<Name of your stack>",
  "attributes": [
    "<Name of attribute 1>",
    "<Name of attribute 12>",
    //...
  ]
}

For example, if you have a container-based API called "MyContainerApi" and a custom resource called "MyCustomResource" that outputs the attributes "CustomAttribute1" and "CustomAttribute2", you backend-config.json and amplify-meta.json files will initially look similar to this:

{
  "api": {
    "MyContainerApi": {
      "apiType": "REST",
      "build": true,
      "dependsOn": [
        {
          "attributes": [
            "ClusterName",
            "VpcId",
            "VpcCidrBlock",
            "SubnetIds",
            "VpcLinkId",
            "CloudMapNamespaceId"
          ],
          "category": "",
          "resourceName": "NetworkStack"
        }
      ],
      "deploymentMechanism": "FULLY_MANAGED",
      "iamAccessUnavailable": true,
      "imageSource": {
        "type": "CUSTOM"
      },
      "providerPlugin": "awscloudformation",
      "resourceName": "graphql",
      "restrictAccess": false,
      "service": "ElasticContainer",
      "skipHashing": false
    }
  },
  "custom": {
    "MyCustomResource": {
      "dependsOn": [],
      "providerPlugin": "awscloudformation",
      "service": "customCloudformation"
    }
  }
}

And the stack name for your custom resource will be something like "customMyCustomResource" (you can check the name on your root stack in CF). Then you need to add the following object to the dependsOn array of your API:

{
  "category": "",
  "resourceName": "customMyCustomResource", // Name of the stack
  "attributes": [
    // Name of the output attributes you want to input
    "CustomAttribute1",
    "CustomAttribute2"
  ],
}

So the file ends up looking like this:

{
  "api": {
    "MyContainerApi": {
      "apiType": "REST",
      "build": true,
      "dependsOn": [
        {
          "attributes": [
            "ClusterName",
            "VpcId",
            "VpcCidrBlock",
            "SubnetIds",
            "VpcLinkId",
            "CloudMapNamespaceId"
          ],
          "category": "",
          "resourceName": "NetworkStack"
        },
        {
          "category": "",
          "resourceName": "customMyCustomResource",
          "attributes": ["CustomAttribute1", "CustomAttribute2"]
        }
      ],
      "deploymentMechanism": "FULLY_MANAGED",
      "iamAccessUnavailable": true,
      "imageSource": {
        "type": "CUSTOM"
      },
      "providerPlugin": "awscloudformation",
      "resourceName": "graphql",
      "restrictAccess": false,
      "service": "ElasticContainer",
      "skipHashing": false
    }
  },
  "custom": {
    "MyCustomResource": {
      "dependsOn": [],
      "providerPlugin": "awscloudformation",
      "service": "customCloudformation"
    }
  }
}

Then run amplify push --force, and you'll see the parameters in your API stack:

"customMyCustomResourceCustomAttribute1": {
  "Type": "String"
},
"customMyCustomResourceCustomAttribute2": {
  "Type": "String"
},

Using the parameters as environment variables for your container

Having the parameters is nice, but since you can't make any manual changes to the file, you can't really reference them, so they're kinda useless. I've discovered how to use them as environment variables for a container. You need to add the attribute "environmentMap" to your containerized API definition like this:

{
  "environmentMap": {
    "CUSTOM_ATTRIBUTE_1": {
      "Ref": "customMyCustomResourceCustomAttribute1"
    },
    "CUSTOM_ATTRIBUTE_2": {
      "Ref": "customMyCustomResourceCustomAttribute2"
    }
  }
}

So the final definition will be:

{
  "api": {
    "MyContainerApi": {
      "apiType": "REST",
      "build": true,
      "dependsOn": [
        {
          "attributes": [
            "ClusterName",
            "VpcId",
            "VpcCidrBlock",
            "SubnetIds",
            "VpcLinkId",
            "CloudMapNamespaceId"
          ],
          "category": "",
          "resourceName": "NetworkStack"
        },
        {
          "category": "",
          "resourceName": "customMyCustomResource",
          "attributes": ["CustomAttribute1", "CustomAttribute2"]
        }
      ],
      "environmentMap": {
        "CUSTOM_ATTRIBUTE_1": {
          "Ref": "customMyCustomResourceCustomAttribute1"
        },
        "CUSTOM_ATTRIBUTE_2": {
          "Ref": "customMyCustomResourceCustomAttribute2"
        }
      }
      "deploymentMechanism": "FULLY_MANAGED",
      "iamAccessUnavailable": true,
      "imageSource": {
        "type": "CUSTOM"
      },
      "providerPlugin": "awscloudformation",
      "resourceName": "graphql",
      "restrictAccess": false,
      "service": "ElasticContainer",
      "skipHashing": false
    }
  },
  "custom": {
    "MyCustomResource": {
      "dependsOn": [],
      "providerPlugin": "awscloudformation",
      "service": "customCloudformation"
    }
  }
}

You will see how the environment variables are created on your CF template, on the containersDefinition section of your Task:

{
  "ContainerDefinitions": [
    {
      "Command": [],
      "EntryPoint": [],
      "Environment": [
        {
          "Name": "CUSTOM_ATTRIBUTE_1",
          "Value": {
            "Ref": "customMyCustomResourceCustomAttribute1"
          }
        },
        {
          "Name": "CUSTOM_ATTRIBUTE_2",
          "Value": {
            "Ref": "customMyCustomResourceCustomAttribute2"
          }
        }
      ],
      // ...rest of the container definition
    }
  ]
}

asungomez avatar Oct 27 '23 08:10 asungomez