cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

AWS::EC2::EBSEncryptionByDefault

Open rdearm1 opened this issue 4 years ago • 1 comments

You can turn on ebs encryption by default in the console for each region via API or SDK but not via CF.

https://docs.amazonaws.cn/AWSEC2/latest/APIReference/API_EnableEbsEncryptionByDefault.html

rdearm1 avatar Sep 04 '19 21:09 rdearm1

Until this is supported by CloudFormation natively, this can be done with a simple Lambda-backed custom resource (below).

Another (potentially "better") way to implement EBS default encryption in an account is AWS Config. There is an AWS-Managed Config Rule ec2-ebs-encryption-by-default and AWS-Managed Runbook / Automation Document AWSConfigRemediation-EnableEbsEncryptionByDefault.

Resources:
  EbsEncryptionByDefault:
    Type: Custom::EbsEncryptionByDefault
    Properties:
      ServiceToken: !Sub '${EbsEncryptionByDefaultLambdaFunction.Arn}'

  EbsEncryptionByDefaultLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Role: !Sub '${EbsEncryptionByDefaultLambdaExecutionRole.Arn}'
      Handler: index.handler
      Timeout: 20
      Runtime: python3.7
      Code:
        ZipFile: !Sub |
          import boto3
          import os
          import cfnresponse

          def handler(event, context):
              try:
                  ec2 = boto3.client("ec2", region_name="${AWS::Region}")
                  res = ec2.enable_ebs_encryption_by_default()
                  res.update(ec2.modify_ebs_default_kms_key_id(KmsKeyId="alias/aws/ebs"))
                  res.pop("ResponseMetadata", None)
                  cfnresponse.send(event, context, cfnresponse.SUCCESS, res)
              except Exception as e:
                  print("Error:", repr(e))
                  cfnresponse.send(event, context, cfnresponse.FAILED, {"Message": "Error"})

  EbsEncryptionByDefaultLambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service: lambda.amazonaws.com
          Action: ['sts:AssumeRole']
      ManagedPolicyArns:
        - !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - ec2:DisableEbsEncryptionByDefault
                  - ec2:EnableEbsEncryptionByDefault
                  - ec2:GetEbsDefaultKmsKeyId
                  - ec2:GetEbsEncryptionByDefault
                  - ec2:ModifyEbsDefaultKmsKeyId
                  - ec2:ResetEbsDefaultKmsKeyId
                Resource: '*'

Outputs:
  EbsEncryptionByDefault:
    Description: The updated status of encryption by default.
    Value:
      !Sub '${EbsEncryptionByDefault.EbsEncryptionByDefault}'
  EbsDefaultKmsKeyArn:
    Description: The Amazon Resource Name (ARN) of the default CMK for encryption by default.
    Value:
      !Sub '${EbsEncryptionByDefault.KmsKeyId}'

atheiman avatar Jan 12 '21 01:01 atheiman

Even though this has shipped, it doesn't appear to be available per the cfnspec (us-east-1). Am I missing something or did this perhaps get incorrectly moved on the roadmap?

tennex-adam avatar Jul 08 '23 12:07 tennex-adam

Even though this has shipped, it doesn't appear to be available per the cfnspec (us-east-1). Am I missing something or did this perhaps get incorrectly moved on the roadmap?

I also don't see this in the spec. Bump for any guidance on where to find this?

loomly-rsuarez avatar Aug 25 '23 14:08 loomly-rsuarez

Where is this?

carpnick avatar Oct 24 '23 15:10 carpnick

Has this feature been implemented? It's frustrating to implement AWS best practices and not have support at the CloudFormation level.

AffiTheCreator avatar Nov 08 '23 15:11 AffiTheCreator

This feature has not yet shipped, it was closed by mistake - sorry for the confusion!

ericzbeard avatar Nov 10 '23 00:11 ericzbeard

This looks like it may have been closed by mistake again. I'm still not able to find any reference in the us-east-1 cfnspec.

tennex-adam avatar Jan 29 '24 14:01 tennex-adam

Confirmation from @ericzbeard from Discord this is not in fact shipped and needs to be reopened.

carpnick avatar Jan 31 '24 19:01 carpnick

Closed by mistake again?

carpnick avatar Mar 05 '24 16:03 carpnick

Yes seems to have been a mistake again --- I am sorry about that :(

nmeisen avatar Mar 05 '24 20:03 nmeisen