Bill
Bill

Reputation: 2993

how to use ImportValue in parameters?

As I knew, I can use ImportValue to reference value from another cloudformation stack in part of Resources.

NetworkInterfaces:
- GroupSet:
  - Fn::ImportValue:
      Fn::Sub: "${NetworkStackNameParameter}-SecurityGroupID"
  AssociatePublicIpAddress: 'true'
  DeviceIndex: '0'
  DeleteOnTermination: 'true'
  SubnetId:
    Fn::ImportValue:
      Fn::Sub: "${NetworkStackNameParameter}-SubnetID"

But seems this feature can't be used in Parameters

Parameters:
  VPC:
    Description: VPC ID
    Type: String
    Default:
      Fn::ImportValue:
        !Sub "${NetworkStackNameParameter}-VPC"

If I use above way, will get the error:

An error occurred (ValidationError) when calling the CreateChangeSet operation: Template format error: Every Default member must be a string.

Anyway to work around? because the same vpc id, subnet id, security group Id, will be used not only one place.

updates

So I have to give up:

  1. In your AWS CloudFormation template, confirm that the Parameters section doesn't contain any intrinsic functions.

https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-template-validation/

Upvotes: 15

Views: 9345

Answers (1)

pbthorste
pbthorste

Reputation: 488

One way of doing this is to use a condition:

Parameters:
  MyValue:
    Type: String
    Value: ''
Conditions:
  MyValueExists: !Not [ !Equals [!Ref MyValue, '']]
Resources:
  Resource:
    Type: AWS::Something
    Properties:
      Key: !If [MyValueExists, !Ref MyValue, !ImportValue 'Imported']

Upvotes: 3

Related Questions