Search code examples
amazon-web-servicesaws-cloudformationamazon-cloudfront

Public key creation in AWS cloudformation giving following error: Invalid request provided: AWS::CloudFront::PublicKey


I have the following Cloudformation template (.yml file) where I am creating Public key to add to a Keygroup, also created in same template. (The public key below is just for example purpose.)

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  CloudfrontPublicKey:
    Type: AWS::CloudFront::PublicKey
    Properties:
      PublicKeyConfig:
        CallerReference: 'some-caller-reference'
        Comment: 'Public key for signed url'
        Name: 'cloudfront-public-key'
        EncodedKey: '-----BEGIN PUBLIC KEY-----aaaabbbb-----END PUBLIC KEY-----'
  CloudFrontKeyGroup:
    Type: AWS::CloudFront::KeyGroup
    Properties:
      KeyGroupConfig:
        Comment: 'Key group for signed url'
        Items:
          - !Ref CloudfrontPublicKey
        Name: 'cloudfront-key-group'

However, I am getting the following error relating to the public key

  Invalid request provided: AWS::CloudFront::PublicKey

The original public key is multi-line, for example:

-----BEGIN PUBLIC KEY-----
aaaa
bbbb
-----END PUBLIC KEY-----

I tried to make it fit in single line by adding new line character as follows

-----BEGIN PUBLIC KEY-----\naaaa\nbbbb\n-----END PUBLIC KEY-----

I also tried without the new line character

-----BEGIN PUBLIC KEY-----aaaabbbb-----END PUBLIC KEY-----

Both attempts did not work, and I am still getting the same error.


Solution

  • Usually, you can use multi-line strings in yaml for that kind of situations. Thus you may try the following version of the template:

    AWSTemplateFormatVersion: "2010-09-09"
    Resources:
      CloudfrontPublicKey:
        Type: AWS::CloudFront::PublicKey
        Properties:
          PublicKeyConfig:
            CallerReference: 'some-caller-reference'
            Comment: 'Public key for signed url'
            Name: 'cloudfront-public-key'
            EncodedKey: |
              -----BEGIN PUBLIC KEY-----
              aaaa
              bbbb
              -----END PUBLIC KEY-----
    
      CloudFrontKeyGroup:
        Type: AWS::CloudFront::KeyGroup
        Properties:
          KeyGroupConfig:
            Comment: 'Key group for signed url'
            Items:
              - !Ref CloudfrontPublicKey
            Name: 'cloudfront-key-group'