Search code examples
openstackopenstack-heatfloating-ip

Assign a floating ip to an openstack instance using a heat template


Here is my template that works. I execute a 'heat stack-create' command with that template and it creates a stack and an instance with an ip. I have access to an interface to manage my instances. From that interface, I am able to create a floating IP and then assign it to my newly created instance.

heat_template_version: 2013-05-23

description: >
    Docker generic server

parameters:
    image_id: {default: centos-7, description: image, type: string}
    instance_type: {default: ug1.medium, description: instance, type: string}
    key_name: {default: user, description: Name of an existing key pair to use for the instance, type: string}

resources:
    nginx_securitygroup:
      properties:
        GroupDescription: Generic security group for nginx stack
        SecurityGroupIngress:
          - {CidrIp: 10.0.0.0/8, FromPort: '80', IpProtocol: TCP, ToPort: '80'}
  type: AWS::EC2::SecurityGroup


server_securitygroup:
    type: AWS::EC2::SecurityGroup
    properties:
        GroupDescription: Generic security group from docker nginx
        SecurityGroupIngress:
            # This is needed to allow pinging the server
            - {"CidrIp": "10.0.0.0/8", "FromPort": "-1", "ToPort": "-1", "IpProtocol": "ICMP"}

            # Ssh port
            - {"CidrIp": "10.0.0.0/8", "FromPort": "22", "ToPort": "22", "IpProtocol": "TCP"}

            # Open docker ports
            - {"CidrIp": "10.0.0.0/8", "FromPort": "2375", "ToPort": "2375", "IpProtocol": "TCP"}
            - {"CidrIp": "10.0.0.0/8", "FromPort": "2376", "ToPort": "2376", "IpProtocol": "TCP"}

docker_server:
    type: AWS::EC2::Instance
    properties:

        ImageId: { get_param: image_id }
        InstanceType: { get_param: instance_type }
        KeyName: { get_param: key_name }
        SecurityGroups:
            - Ref: server_securitygroup
            - {Ref: nginx_securitygroup}


        UserData: |
            #!/bin/bash -v

            #Do some operations to start the docker container on the instance                

outputs:
    ipaddress_private:
        description: Private ip
        value:
            "Fn::GetAtt":
                - docker_server
                - PrivateIp

My issue is that I do not want to assign manually my created floating IP to the instance, I want it to be assigned automatically when my stack and instance are created. I tried following some documentatio, this one for example: http://blog.oddbit.com/2013/12/06/an-introduction-to-openstack-heat/

But it does not work. Maybe it's because it's trying to assign a floating IP to another existing resource (server). How can I make the association work?


Solution

  • Ok, by trial and error, I found what to do...

    In the 'parameters' section, add:

    ip_address : {default: x.x.x.x, description : Floating IP address to be associated to the instance, type : string}
    

    In the 'resources' section:

    IPAssoc :
        type : AWS::EC2::EIPAssociation
        properties :
            InstanceId : { get_resource: docker_server }
            EIP : { get_param : ip_address }