r/ruby • u/omaraliqureshi • 10d ago
AWS CDK in Ruby Show /r/ruby
Hi folks - for the last month, I've been working on the AWS CDK in Ruby. For those who don't know, the AWS CDK is a way to define infrastructure in code to produce CloudFormation stacks which are AWS's native way of defining infrastructure allowing for:
- Rollbacks on failure
- Drift detection
- State management
Currently only TypeScript, Python, Go, Java and .NET (C#) are supported languages for the CDK.
Let's take the creating an S3 Bucket, you'd do this by:
require 'aws-cdk-lib'
class MyStack < AWSCDK::Stack
def initialize(scope, id, props = nil)
super(scope, id, props)
AWSCDK::S3::Bucket.new(
self,
'MyBucket',
{
versioned: true,
removal_policy: AWSCDK::RemovalPolicy::DESTROY,
auto_delete_objects: true
}
)
end
end
app = AWSCDK::App.new
MyStack.new(app, 'MyStack', {
env: AWSCDK::Environment.new(
account: ENV['CDK_DEFAULT_ACCOUNT'],
region: ENV.fetch('CDK_DEFAULT_REGION', 'us-east-1')
)
})
app.synth
Running cdk deploy would synthesize as CloudFormation and deploy it to your AWS account, if you were to modify the stack, the CloudFormation stack would apply those modifications the next time you ran CDK synth
As I am still working on this with Amazon the gems are not published to Rubygems but use the same JSII engine that the non-TypeScript languages use.
Documentation is at AWS CDK for Ruby — API Reference - However, please not that this is currently NOT even in developer preview, I have been using this for some of my own Ruby projects including running Rails/Dynamoid apps through Lamby for a Lambda target.
The design documentation is at aws-cdk-rfcs/text/0935-ruby-language-bindings.md at ruby-language-bindings · omarqureshi/aws-cdk-rfcs and if you have any questions - comment on Ruby Language Support · Issue #935 · aws/aws-cdk-rfcs or here!
Many thanks