Search code examples
amazon-web-servicesamazon-ec2aws-cloudformationip-addressipsec

Cloudformation AWS: Assign an ip address to a CustomerGateway using parameters


I'm designing a cloudformation template, but I need to define the source IP as a Parameter

I tried to define the parameter as a String but it produces the following error:

Value (${MyCustomerGateway}) for parameter ipAddress is invalid. Invalid Format. (Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterValue; Request ID: 4de02112-fb1f-47a1-931c-97727568df99)

this is the fragment of the template:

Parameters:
  MyCustomerGateway:
    Description: IpAddress.
    Default: 0.0.0.0
    Type: String  

Resources:
  CustomerGateway_1:
    Type: 'AWS::EC2::CustomerGateway'
    Properties:
      Type: ipsec.1
      BgpAsn: 3352
      IpAddress: ${MyCustomerGateway} 
      Tags:
        - Key: Name
          Value: CustomerGateway_1

Is there any special data for the IP translation?

I'm not sure which is the right way


Solution

  • Change IpAddress: ${MyCustomerGateway} to IpAddress: !Ref myCustomerGateway

    More about Ref :

    The intrinsic function Ref returns the value of the specified parameter or resource.

    > When you specify a parameter's logical name, it returns the value of the parameter.

    When you specify a resource's logical name, it returns a value that you can typically use to refer to that resource, such as a physical ID.

    Bonus:

    In case you want to validate parameter inputs, you can use the AllowedPattern property.

    Usage:

    PrimaryIPAddress:
        Type: String
        Description: This must be a valid IP address.
        AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
        ConstraintDescription: must be a valid IP address of the form x.x.x.x.
    

    Error when entering invalid IP:

    enter image description here