跳至內容

Include Parameters in an AWS Include file

0

I am getting an error saying "Transform AWS::Include failed with: The specified S3 object's content should be valid Yaml/JSON" when I am trying to deploy.

I want to split few resources to another include file from my main template file as the size of my main file has exceeded 50 KB.

But as the parameters are present in main file and not getting referred in the include file I am getting the error mentioned above. Below is sample of my main file--

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Parameters:
  UsgPlanReminderResS3Location:
    Type: String
  SupportEmailAddress:
    Type: String
    Description: Email address to send usage plan alert to
    ....
Conditions:
    ....
Description:
    ....
Resources:
    DomainBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Condition: EnableDa
    Properties: 
      BasePath: "account"
      DomainName: !Ref apiDomainName
      RestApiId: !Ref LoanAccountApi
      Stage: !Ref LoanAccountApiStage
    'Fn::Transform':
    Name: 'AWS::Include'
    Parameters:
      Location: !Ref UsgPlanReminderResS3Location
Outputs:
    ....

Below is my include file with other resources--

UsagePlanReminderTopic:
    Type: AWS::SNS::Topic

  UsagePlanReminderSubscription:
    Type: AWS::SNS::Subscription
    Properties: 
      Protocol: email
      Endpoint: !Ref SupportEmailAddress
      TopicArn: !Ref UsagePlanReminderTopic
   ..... 

As seend above, the issue is due to "SupportEmailAddress" not getting resolved. How do I pass the paramters from main to include file?

Thanks

已提問 2 年前檢視次數 449 次

3 個答案
0

To resolve the issue you're facing with passing parameters from your main template to the include file, you need to modify your approach slightly. The AWS::Include transform doesn't automatically pass parameters from the main template to the included file. Instead, you'll need to pass the parameters explicitly.

Here's how you can modify your main template to pass the required parameters to the include file:

  1. In your main template, modify the AWS::Include transform section to pass the necessary parameters:
'Fn::Transform':
  Name: 'AWS::Include'
  Parameters:
    Location: !Ref UsgPlanReminderResS3Location
    SupportEmailAddress: !Ref SupportEmailAddress
  1. In your include file, you need to declare the parameters at the top, similar to how you would in a regular CloudFormation template:
Parameters:
  SupportEmailAddress:
    Type: String

Resources:
  UsagePlanReminderTopic:
    Type: AWS::SNS::Topic

  UsagePlanReminderSubscription:
    Type: AWS::SNS::Subscription
    Properties: 
      Protocol: email
      Endpoint: !Ref SupportEmailAddress
      TopicArn: !Ref UsagePlanReminderTopic

By making these changes, you're explicitly passing the SupportEmailAddress parameter to the include file, and then declaring it within the include file so it can be used.

Remember that the include file should be a valid CloudFormation template snippet on its own. It should contain only the sections you want to include (like Parameters and Resources), but it doesn't need the AWSTemplateFormatVersion or Description sections.

This approach should resolve the "Transform AWS::Include failed" error you're encountering and allow you to successfully split your resources into separate files while still being able to use parameters defined in the main template.
Sources
s3 bucket i m getting an error please help me to resolve this | AWS re:Post
Cloud formation Stacks creating and deploying fail S3 access error. | AWS re:Post

已回答 2 年前

  • I tried with the auto suggested solution of passing parameters to include file. But still after passing all the required parameters, I am getting the same error. Do I need to pass "location" parameter too to include file? It is is no use there.

0

Hi there,

looking at the official documentation here [ 1 ], it is mentioned:

"You can use the AWS::Include transform anywhere within the CloudFormation template except in the template parameters section or the template version field".

At the moment, leveraging the AWS:Include type cannot be passed as a parameter even after making use of the changes shared above.

A workaround you can look into would be to make use of exports and outputs following the document here [ 2 ] [ 3 ]

References:

[ 1 ] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/create-reusable-transform-function-snippets-and-add-to-your-template-with-aws-include-transform.html#:~:text=You%20can%20use%20the%20AWS%3A%3AInclude%20transform%20anywhere%20within%20the%20CloudFormation%20template%20except%20in%20the%20template%20parameters%20section%20or%20the%20template%20version%20field

[ 2 ] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html

[ 3 ] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html

AWS

已回答 2 年前

0

Hi

As per the template snippets that you provided, you encountered the error Transform AWS::Include failed with: The specified S3 object's content should be valid Yaml/JSON because your AWS::Include boilerplate content includes short hand syntax of Ref intrinsic function.

As per the document here, AWS::Include transform currently does not support shorthand notations for YAML snippets. Your AWS::Include template snippet should look like as shown below:

UsagePlanReminderTopic:
  Type: AWS::SNS::Topic

UsagePlanReminderSubscription:
  Type: AWS::SNS::Subscription
  Properties: 
    Protocol: email
    Endpoint:
      Ref: SupportEmailAddress
    TopicArn:
      Ref: UsagePlanReminderTopic
   ..... 

Thanks

AWS
支援工程師

已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。