Resource creation cancelled Resource handler returned message: "The pending-instance-creation instance to which 'eni is attached is not in a valid state for this operation

0

Hi , iam trying create a vm using cft which deploys a vm which is getting attached with 2 network interfaces and specified ip address and the issue is while deploying the stacks out of 10 stack creation i get to face 2-3 getting afiled due to this error message : Resource creation cancelled Resource handler returned message: "The pending-instance-creation instance to which 'eni-0f53323d885d5f614' is attached is not in a valid state for this operation (Service: Ec2, Status Code: 400, Request ID: 086f97ee-ef27-4335-bbde-fb9e773b06b8)" (RequestToken: 877234da-3497-7728-5e42-afb285ca40e7, HandlerErrorCode: GeneralServiceException)

template syntax :

"NetworkInterface1": {
            "Type": "AWS::EC2::NetworkInterface",
            "Properties": {
                "SubnetId": {
                    "Ref": "EC2InstanceSubnet"
                },
                "Description": "First network interface",
                "PrivateIpAddress": "192.168.172.10",
                "SourceDestCheck": false,
                "GroupSet": [
                    {
                        "Ref": "EC2InstancesVPCSSH"
                    }
                ],
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "NetworkInterface1"
                    }
                ]
            }
        },
        "NetworkInterface2": {
            "Type": "AWS::EC2::NetworkInterface",
            "Properties": {
                "SubnetId": {
                    "Ref": "EC2InstanceSubnet"
                },
                "Description": "Second network interface",
                "PrivateIpAddress": "192.168.172.20",
                "SourceDestCheck": false,
                "GroupSet": [
                    {
                        "Ref": "EC2InstancesVPCSSH"
                    }
                ],
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "NetworkInterface2"
                    }
                ]
            }
        },
        "ElasticIP1": {
            "Type": "AWS::EC2::EIP",
            "Properties": {
                "Domain": "vpc"
            }
        },
        "EIPAssociation1": {
            "Type": "AWS::EC2::EIPAssociation",
            "Properties": {
                "NetworkInterfaceId": {
                    "Ref": "NetworkInterface1"
                },
                "AllocationId": {
                    "Fn::GetAtt": [
                        "ElasticIP1",
                        "AllocationId"
                    ]
                }
            }
        },
        "MYVM": {
            "Type": "AWS::EC2::Instance",
            "DependsOn": [
                "NetworkInterface1",
                "NetworkInterface2",
                "ElasticIP1"
            ],
            "Properties": {
                "ImageId": {
                    "Fn::FindInMap": [
                        "AMIIDMap",
                        {
                            "Ref": "AWS::Region"
                        },
                        "myvmAMIID"
                    ]
                },
                "InstanceType": "t3.xlarge",
                "NetworkInterfaces": [
                    {
                        "NetworkInterfaceId": {
                            "Ref": "NetworkInterface1"
                        },
                        "DeviceIndex": 0
                    },
                    {
                        "NetworkInterfaceId": {
                            "Ref": "NetworkInterface2"
                        },
                        "DeviceIndex": 1
                    }
                ],


i would neeed you help to solving the issue as i have tried out several ways to fix it but i can anyone help me with the solution i would be happy to share the template if required

thanks

2 Answers
2

The error you're seeing suggests that an Elastic IP (EIP) can't be linked to a network interface because the associated instance isn't ready yet - it's likely still in the "pending" state. This happens when the instance hasn't finished initializing, preventing the EIP from being attached.

To fix this, you'll need to tweak your CloudFormation template. The key is to use the DependsOn attribute, making sure the AWS::EC2::EIPAssociation resource waits for the AWS::EC2::Instance resource to be ready. This ensures the instance is fully up and running before the template tries to associate the EIP.

By adding this dependency, you're essentially telling CloudFormation to hold off on the EIP association until the instance is good to go. This should solve your error and make your stack deployments more reliable.

Updated CloudFormation template snippet:

        "EIPAssociation1": {
            "Type": "AWS::EC2::EIPAssociation",
             "DependsOn": [
                "MYVM"
            ],
            "Properties": {
                "NetworkInterfaceId": {
                    "Ref": "NetworkInterface1"
                },
                "AllocationId": {
                    "Fn::GetAtt": [
                        "ElasticIP1",
                        "AllocationId"
                    ]
                }
            }
        },
AWS
answered 12 days ago
profile picture
EXPERT
reviewed 11 days ago
1

The error you're encountering during CloudFormation stack creation is related to the EC2 instance and its network interface (ENI) attachment. This issue occurs intermittently, which suggests it might be related to timing or resource availability during the deployment process.

To address this problem, you can try the following approaches:

  1. Implement a retry mechanism: Add a DependsOn attribute to your EC2 instance resource in the CloudFormation template. This will ensure that the network interfaces are fully created before the instance is launched.

  2. Use a creation policy: Apply a creation policy to your EC2 instance resource. This will make CloudFormation wait for a specified number of success signals before considering the resource creation complete.

  3. Increase the timeout: If the issue is related to timing, you can increase the timeout for your stack creation. This gives more time for resources to be properly created and attached.

  4. Use a custom resource: Create a custom resource in your CloudFormation template that checks the status of the network interfaces before proceeding with the instance creation.

  5. Implement error handling: In your CloudFormation template, add error handling and automatic rollback configurations to manage failures gracefully.

  6. Check service quotas: Ensure that you haven't reached any service quotas for EC2 instances or ENIs in your AWS account.

If the problem persists after trying these solutions, you may want to review your CloudFormation template for any potential issues with the EC2 instance or network interface configurations. Additionally, consider reaching out to AWS support for further assistance, as they may be able to provide more specific guidance based on your account and configuration details.

Recent improvements in AWS CloudFormation, such as faster stack creation and new event types like CONFIGURATION_COMPLETE, may also help in resolving such issues by allowing for better parallel resource creation and improved error detection. Keep your AWS CLI and SDKs updated to take advantage of these enhancements.
Sources
Accelerate AWS CloudFormation troubleshooting with Amazon Q Developer assistance - AWS
Experience up to 40% faster stack creation with AWS CloudFormation

profile picture
answered 12 days ago
profile picture
EXPERT
reviewed 11 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions