DEV Community

Cover image for ⚡️Combining Serverless Framework & AWS CDK

⚡️Combining Serverless Framework & AWS CDK

Sebastian Bille on October 25, 2021

Serverless Framework is a fantastic tool for defining and managing your Lambda functions. But let's face it; it's not very good at handling other t...
Collapse
 
fredericbarthelet profile image
Frédéric Barthelet • Edited

Nice article ! Thanks for sharing.
In addition, if your CDK stack does not require any assets to be uploaded (understand does not contain any lambda, which should be taken care of by Serverless framework definition), you can use CDK programmatic API synth to inject generated Cloudformation resources directly within the resources key of your serverless.yml file. You then only need to run sls deploy to deploy both Serverless framework resources and CDK resources. Take a look at twitter.com/bartheletf/status/1382... to know more !

Collapse
 
tastefulelk profile image
Sebastian Bille

Ooh, that's a great point I didn't consider! It does make it a bit simpler but you also lose a few of the benefits of having the separate stacks and being able to cdk diff for example, I suppose.

Thanks for the tip! 🙌

Collapse
 
adityanair102001 profile image
AdityaNair102001

Hi @Frédéric, excellent idea but I had a question:
What if the exported values are tokens? Cause the token values are alloted actual values only after deployment through cdk.

Collapse
 
fredericbarthelet profile image
Frédéric Barthelet

Hi @adityanair102001 , I wrote an article to explain in more detail what can be achieved in terms of CDK provisioning through Serverless framework lifecycle. You can have a look at dev.to/kumo/serverless-framework-a... to learn more. Token can be resolved using the resolve API available on the CDK stack :)

Collapse
 
andrenbrandao profile image
André Brandão

Thanks for sharing! Having to reference the queue with a simple {cf:InfrastructureStack.queueUrl} is much better than making complex Joins in CloudFormation.

In one of my projects, I was having to reference a queue like this:

      MESSAGE_PROCESSING_QUEUE_URL: {
        'Fn::Join': [
          '',
          [
            'https://sqs.',
            { Ref: 'AWS::Region' },
            '.',
            { Ref: 'AWS::URLSuffix' },
            '/',
            { Ref: 'AWS::AccountId' },
            '/',
            { 'Fn::GetAtt': ['MessageProcessingQueue', 'QueueName'] },
          ],
        ],
      }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tastefulelk profile image
Sebastian Bille

Ah, I've had my fair share of those! Happy you found it useful! 🙌

Collapse
 
memark profile image
Magnus Markling

The "cd infrastructure && yarn cdk" trick was nice!

Collapse
 
janvanthoor profile image
janvt

Fantastic! Cleverly combines 2 awesome things :) Bookmarked for later, this actually is a wheel I have invented many times before with varying levels of pain.

Collapse
 
tastefulelk profile image
Sebastian Bille

Glad you liked it! 🙌

Collapse
 
grenguar profile image
Igor Soroka

Sounds interesting! I think it could be helpful if you want to eventually migrate to CDK completely.

Collapse
 
tastefulelk profile image
Sebastian Bille

I agree! And if not, in one way or another we'll likely some kind of support in Serverless to use CDK constructs in the future and this could bridge the gap until then!