Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template format error: Unresolved resource dependencies

I try to create an EC2 instance with the template below:

Parameters:
  KeyName:
    Default: TestKeyPair
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: AWS::EC2::KeyPair::KeyName
Resources:
  Dev:
    Properties:
      ImageId: ami-4e79ed36
      InstanceType: t2.micro
      KeyName: !Ref 'KeyName'
      SecurityGroups:
        - !Ref 'SSH'
    Type: AWS::EC2::Instance

but I get:

An error occurred (ValidationError) when calling the CreateChangeSet operation: Template format error: Unresolved resource dependencies [SSH] in the Resources block of the template

I can't understand what's wrong in the template since the security group named "SSH" is already present:

$ aws ec2 describe-security-groups --group-names SSH
....
"IpPermissions": [
    {
        "ToPort": 22,
        "IpRanges": [
            {
                "CidrIp": "0.0.0.0/0"
            }
        ],
        "FromPort": 22,
        "IpProtocol": "tcp",
        "UserIdGroupPairs": [],
        "PrefixListIds": [],
        "Ipv6Ranges": []
    }
],
"GroupName": "SSH",
"GroupId": "sg-3b8bc345",
"Description": "Enable SSH access via port 22",
"OwnerId": "150811659115",
"VpcId": "vpc-a84688cf"
....
like image 330
alessmar Avatar asked Jun 05 '18 04:06

alessmar


People also ask

How to fix “unresolved resource dependencies[xxxxxxxx] in the resources block of the template”?

For “Unresolved resource dependencies [XXXXXXXX] in the Resources block of the template” errors, we perform this. 1. First, we confirm that resource logical IDs are defined in the template. 2. Also, we need to confirm that resource physical IDs exist in the environment.

Why is my CloudFormation template invalid?

“The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template” When you define a resource in your serverless.yml, you can reference an attribute of another resource dynamically.

How do I resolve the [environmental resource]'xxxxxxxx'error?

For "The [environmental resource] 'XXXXXXXX' does not exist" errors, see the Verify that your resource exists outside the stack, or validate dependencies for resources in the same stack section. For "At least one Resources member must be defined" errors, see the Include a Resources section in your template section.

Why can’t I reference a resource in my resources?

When you define a resource in your serverless.yml, you can reference an attribute of another resource dynamically. This error happens when the referenced resource cannot be found. Ensure you are referring to the logical ID of the resource. For example, if you define an S3 bucket in your resources:


1 Answers

!Ref only works for Logical ID that exists within the template. That doesn't mean that you can't reference an existing security group, that just mean that you'll have to reference it in some other way. For your particular use case I suggest you pass the security group as a stack parameter like so:

Parameters:
  KeyName:
    Default: TestKeyPair
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: AWS::EC2::KeyPair::KeyName
  SSHSecurityGroup:
    Description: SecurityGroup that allows access to the instance via SSH
    Type: AWS::EC2::SecurityGroup::Id
Resources:
  Dev:
    Properties:
      ImageId: ami-4e79ed36
      InstanceType: t2.micro
      KeyName: !Ref 'KeyName'
      SecurityGroups:
        - !Ref SSHSecurityGroup
    Type: AWS::EC2::Instance

On the stack creation you just have to pass the SSH Security Group in the appropriated field.


That being said, you won't have a much dynamic setup if you do it this way. You should either define the security group within this template and reference it directly (using !Ref), or you could create a template that manages all security groups and use the Export/Import feature of CloudFormation to reference the security groups between stacks.

like image 169
Laurent Jalbert Simard Avatar answered Sep 18 '22 16:09

Laurent Jalbert Simard