5 ways to cost optimize your Infrastructure as Code
Learn five code snippets that you can add to your existing AWS CloudFormation templates to prevent cost waste.
- Amazon CloudWatch Log Group Retention
- Amazon Simple Storage Service (Amazon S3) Lifecycle rules for unused objects
- AWS Graviton for AWS Managed Services
- gp3 for Amazon Elastic Block Store (Amazon EBS) volumes
- Amazon Elastic File System (Amazon EFS) Infrequent Access
Never expire
which means you will store, and more importantly, pay for those logs forever! But if you create the CloudWatch log group upon resource provisioning, then you can define the retention period yourself.1
2
3
4
5
LambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${LambdaFunctionResource}"
RetentionInDays: 14
- Delete Markers - A delete marker in Amazon S3 is a placeholder (or marker) for a versioned object that was requested to be deleted when a bucket has versioning-enabled. The object will not be deleted in this situation, but the delete marker makes Amazon S3 behave as if it is deleted. You can end up storing and paying for hundreds or thousands of previous versions that you thought were deleted.
- Multi Part Uploads (MPUs) - Amazon S3’s multipart upload feature allows you to upload a single object to an S3 bucket as a set of parts. If the complete multipart upload request isn’t sent successfully, Amazon S3 will not assemble the parts and will not create any object. The parts remain in your Amazon S3 account until the multipart upload completes or is aborted, and you pay for the parts that are stored in Amazon S3.
mybucket
with your unique bucket name.1
2
3
4
5
6
7
8
9
10
11
S3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName:"mybucket"
LifecycleConfiguration:
Rules:
- Id: delete-incomplete-mpu-7days
Prefix: ''
AbortIncompleteMultipartUpload:
DaysAfterInitiation: 7
ExpiredObjectDeleteMarker: True
1
2
3
4
5
6
7
8
LambdaFunctionResource:
Type: AWS::Lambda::Function
Properties:
FunctionName: MyLambdaFunction
Description: LambdaFunction of python3.10
Runtime: python3.10
Architectures:
- "arm64"
1
2
3
4
5
6
BlockDeviceMappings:
- DeviceName: "/dev/sdm"
Ebs:
VolumeType: "gp3"
DeleteOnTermination: "true"
VolumeSize: "10"
1
2
3
4
5
6
FileSystemResource:
Type: 'AWS::EFS::FileSystem'
Properties:
LifecyclePolicies:
- TransitionToIA: AFTER_30_DAYS
- TransitionToPrimaryStorageClass: AFTER_1_ACCESS
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.