AWS CloudFront Public Key

This page shows how to write Terraform and CloudFormation for CloudFront Public Key and write them securely.

aws_cloudfront_public_key (Terraform)

The Public Key in CloudFront can be configured in Terraform with the resource name aws_cloudfront_public_key. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L11
resource "aws_cloudfront_public_key" "example" {
  comment     = "test public key"
  encoded_key = file("public.pem")
  name        = "test_key"
main.tf#L1
resource "aws_cloudfront_public_key" "cloudfront_public_key" {
  count       = length(var.cloudfront_public_key)
  encoded_key = file(join(".", [join("/", [path.cwd, "key", lookup(var.cloudfront_public_key[count.index], "encoded_key")]), "pem"]))
  name        = lookup(var.cloudfront_public_key[count.index], "name", null)
  comment     = lookup(var.cloudfront_public_key[count.index], "comment", null)
cloudfront_public_key.tf#L4
resource "aws_cloudfront_public_key" "cloudfront_public_key" {
  count = var.enable_cloudfront_public_key ? 1 : 0

  encoded_key = var.cloudfront_public_key_encoded_key

  comment     = var.cloudfront_public_key_comment
cloudfront_public_key.tf#L4
resource "aws_cloudfront_public_key" "cloudfront_public_key" {
  count = var.enable_cloudfront_public_key ? 1 : 0

  encoded_key = var.cloudfront_public_key_encoded_key

  comment     = var.cloudfront_public_key_comment
main.tf#L7
resource "aws_cloudfront_public_key" "this" {
  comment     = var.comment
  encoded_key = var.encoded_key
  name        = var.name
  name_prefix = var.name_prefix
}

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Tips: Best Practices for The Other AWS CloudFront Resources

In addition to the aws_cloudfront_distribution, AWS CloudFront has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_cloudfront_distribution

Ensure to enable access logging of CloudFront distribution

To avoid attacks, it is better to configure access logging of a CloudFront distribution. The logs are important for the early-stage detection of attacks and incident responses. It is better to enable the feature while being careful of handling cookies.

Review your AWS CloudFront settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

AWS::CloudFront::PublicKey (CloudFormation)

The PublicKey in CloudFront can be configured in CloudFormation with the resource name AWS::CloudFront::PublicKey. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

Explanation in CloudFormation Registry

A public key that you can use with signed URLs and signed cookies, or with field-level encryption.

Frequently asked questions

What is AWS CloudFront Public Key?

AWS CloudFront Public Key is a resource for CloudFront of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS CloudFront Public Key?

For Terraform, the chimbs86/Security-And-Microservices-On-AWS, mikamakusa/terraform and SebastianUA/terraform-aws-cloudfront source code examples are useful. See the Terraform Example section for further details.