Search code examples
amazon-web-serviceskotlinaws-lambdacontinuous-integrationjetbrains-space-automation

JetBrains Space Deploy to AWS Lambda


We are experimenting with Jetbrains Space as our code repo and CI/CD. We are trying to find a way to setup the .space.kts file to deploy to AWS Lambda.

We want the develop branch to publish to the Lambda $Latest and when we merge to the main branch from the develop branch we want it to publish a new Lambda version and link that version to the alias pro.

I've looked around but haven't found anything that would suggest there is a pre-built solution for controlling AWS Lambda so my current thinking is something like this:

job("Publish to Lambda Latest") {

    startOn {
        gitPush {
            branchFilter {
                +"refs/heads/develop"
            }
        }
    }

    container(displayName = "AWS Lambda CLI", image = "amazon/aws-cli") {
        
        // Space Packages repository
        env["REPOSITORY_URL"] = "https://..."

        shellScript {
            content = """
                echo Deploying to Lambda Latest...
                ...
            """
        }
    }
}

I'm not convinced that using a shell script is a very robust way to do this as I will need to pass variables from one command to another. Is there a better way to do this?


Solution

  • There is no built-in DSL for interacting with AWS.

    If you want a solution that is more type-safe than plain shellScript, and maybe reuse data between multiple calls etc, you can still use Kotlin code directly (in a kotlinScript block instead of shellScript).

    You can specify maven dependencies for your .space.kts script via the @DependsOn annotation, which you can use for instance to add modules from the AWS Java SDK:

    // you can add other module artifacts like "s3", "ec2", etc.
    @file:DependsOn("software.amazon.awssdk:lambda:2.13.7")
    
    import software.amazon.awssdk.services.lambda.*
    import software.amazon.awssdk.services.lambda.model.*
    
    job("Publish to Lambda Latest") {
    
        startOn {
            gitPush {
                branchFilter {
                    +"refs/heads/develop"
                }
            }
        }
    
        container(displayName = "AWS Lambda", image = "openjdk:11") {
    
            kotlinScript { spaceApi ->
                // use AWS SDK classes here, for instance:
                val client = LambdaClient.builder().build()
                val updateFunctionCodeRequest = UpdateFunctionCodeRequest.builder()
                    .functionName("name")
                    .s3Bucket("bucket")
                    .s3Key("key")
                    .publish(true)
                    .build()
                client.updateFunctionCode(updateFunctionCodeRequest)
                client.close()
            }
        }
    }
    

    The piece of code shown here is just for the sake of the example (to show that you can use the AWS SDK classes and methods), it's not a complete example on how to publish a lambda.