compose icon indicating copy to clipboard operation
compose copied to clipboard

[RFC] Conditionally deploy services

Open chris-hinds opened this issue 3 years ago • 7 comments

Use case description

Right now if you want to conditionally deploy infrastructure say based on the stage you need to add a conditional to each resource is your serverless config files.

If you are using serverless compose it would make this much easier as a single top-level conditional could skip the service deployment.

Proposed solution

services:

  infra-prod:
    path: services/infra-prod
    if: ${sls:stage} == 'prod'

chris-hinds avatar Apr 26 '22 09:04 chris-hinds

We would absolutely use a feature like this if it supported conditions based on region. At the moment we are doing this with environment variables, so as long as --region isn't supported by serverless compose it would be great if something like if: ${env:region} == 'ap-southeast-2' was also possible.

tom-dq avatar May 31 '22 06:05 tom-dq

@tom-dq very interesting, thank you for providing another use case. That will be helpful if we start working on that to make sure the syntax/feature supports this.

To anyone reading this: I'd be interested to learn more about your use case. So far the typical use case we have is: "deploy service X only in production".

Let us know if you have other use cases.

mnapoli avatar May 31 '22 07:05 mnapoli

We have a use case where we only need specific infrastructure deployed in one region. We currently do this by deploying our services separately with GH actions. It would be awesome to be able to define services that are only used in X regions. An example for us is our auth service which manages and contains our Cognito instance and some related hooks, which we currently maintain in one region and share.

bodhihawken avatar Jun 01 '22 04:06 bodhihawken

Thanks @mnapoli - after reading @Hawki101's comment I should say, if serverless compose could natively support multi-region deploys, that would be a next-level developer experience for us. We have an architecture where most of the services are in multiple regions, but a few are global. So a great implementation for us would be:

  • Define a number of services, and the regions to which they should be deployed.
  • Allow the output of some services to be passed as a parameter to a dependent service, even if it's in another region.

I'm not sure if this has crept beyond the scope of the original issue - sorry if it has!

tom-dq avatar Jun 03 '22 02:06 tom-dq

Yeah, this would be a better solution for us rather than deploying compose multiple times across regions and conditionally deploying services.

bodhihawken avatar Jun 03 '22 02:06 bodhihawken

I was also thinking about having a service/stack in my Compose file, that would allow to do deploy a feature branch for some infrastructure. I obviously do not want the feature branch to run on normal PRs or merges, but thinking about running it when someone commits to a branch named feature* that I could just pass in as the stage name.

I would also like to maybe append to the initial request because I could use this same IF statement so that I can conditionally deploy a stack as a feature branch.

stephenbawks avatar Nov 07 '22 20:11 stephenbawks

In my application, I'm using an authoriser Lambda function specifically for non-prod environments. Since this function is deployed as a Lambda@Edge it needs to be in us-east-1 which is a different region to the rest of my application, so it must be in a separate Serverless stack from the rest of my application.

I use serverless-compose to orchestrate these two stacks. It passes the authoriser's ARN to my main stack so that it can be attached to the Cloudfront distribution in my non-prod environments.

Since my production environment is public it doesn't need the authoriser function. This is a challenge in serverless-compose without conditional logic.

My current workaround involves deploying the authoriser function to all environments (including production) and using Cloudformation logic in the main application's Serverless service to prevent the authoriser from being attached in the production environment.

I tried using the serverless-plugin-conditional-functions plugin to deploy the authoriser function only in non-prod environments, but it's not possible to deploy the application to production in this way because Serverless complains about the missing parameter (since the ARN of the authoriser function doesn't exist in the production environment)

Having conditional logic available in serverless-compose would significantly simplify and this architecture and make it more efficient.

calebplum avatar Jan 17 '24 10:01 calebplum