Search code examples
amazon-web-servicesboto3elastic-ip

How to add elastic ip name in AWS via Boto3 API?


When creating the elastic IP in AWS via boto3. API does not give an option to add Name to the elastic IP but this field is available via UI. How can I add Name to elastic IP while creating it or after?

enter image description here

Following code work:

import boto3

client = boto3.client('ec2')
addr = client.allocate_address(Domain='vpc')
print addr['PublicIp']

But, if I add "name" field, it throws this error:

ParamValidationError: Parameter validation failed: Unknown parameter in input: "Name", must be one of: DryRun, Domain


Solution

  • What you see there is a tag. It doesn't appear that Elastic IPs support "Tag-On-Create", so you have to create the tag after the EIP has been created.

    Try the following:

    import boto3
    
    client = boto3.client('ec2')
    addr = client.allocate_address(Domain='vpc')
    print(addr['PublicIp'])
    
    response = client.create_tags(
        Resources=[
            addr['AllocationId'],
        ],
        Tags=[
            {
                'Key': 'Name',
                'Value': 'production',
            },
        ],
    )
    print(response)