Skip to content

Document capture of Outputs and sls info results #26

Description

@matthewpoer

This took a while to figure out so I'd like to share and advise that it'd be nice to see in the README or elsewhere in the documentation.

My use case was that I needed to capture some of my resources.Outputs from a serverless deployment for use with later scripting. For example, here's an excerpt from my serverless.yml file:

resources:
  Resources:
    host:
      Type: AWS::EC2::Instance
      Properties:
        ImageId: ami-0f2b4fc905b0bd1f1
        InstanceType: t2.medium
  Outputs:
    PublicDnsName:
      Description: "host public URL"
      Value: !GetAtt host.PublicDnsName

With that in place I can run sls info --verbose and capture in that output is the desired PublicDnsName but I have to grep and adjust for that, e.g.

sls info --verbose | grep PublicDnsName | cut -c 16-

All that said, I found it difficult to perform that scrub-and-grab sequence in a Github Action using this container, but finally landed on overwriting the entrypoint and prefixing my command with a -c, which is appended as an argument to the bash invocation. It's a bit confusing to me still, but it's functional:

name: Deploy to Amazon EC2
on:
  push:
    branches:
      - master
jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Run serverless deployment
      uses: serverless/github-action@v1.53.0
      with:
        args: deploy --stage=dev --verbose
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }}
        SLS_DEBUG: 1
    - name: Export sls/aws-ec2 host
      uses: serverless/github-action@v1.53.0
      with:
        args: -c "serverless info --stage=dev --verbose | grep PublicDnsName | cut -c 16- > host.txt"
        entrypoint: /bin/bash
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }}
        SLS_DEBUG: 1
    - name: Show site URL for review and testing
      run: |
        export host=$(cat host.txt)
        echo http://$host/

Activity

  1. DavidWells commented on Apr 6, 2020

    @DavidWells

    Solid tip

    I made a plugin that outputs Outputs into a "manifest file" https://www.npmjs.com/package/serverless-manifest-plugin

    This might help avoid some hardcore greps.

    I'd love if sls deploy had a --json flag to output a simple structure to parse with jq

  2. matthewpoer commented on Apr 7, 2020

    @matthewpoer
    Author

    @DavidWells thanks, and nice plugin. I think we've identified the same need. The first time I saw you could set output params. this way in sls I thought it was great. When I found the only way to access them was re-running sls, requiring re-authorization and again setting the stage and the verbose flag, then having to dig for the result, I was not pleased.

  3. marcelloromani commented on Jan 7, 2022

    @marcelloromani

    Thank you @matthewpoer

    Just want to add that I'm using v2.18.0 and had to change the shell to /bin/sh.

  4. miltonhit commented on Sep 7, 2023

    @miltonhit

    Thank you so much for this plugin!!!
    My github workflow looks like this, and worked fine!

      deploy-backend:
        needs: build
        runs-on: ubuntu-latest
        environment:
          name: aws
          url: ${{ steps.manifest.outputs.url }}
    
        steps:
          - uses: actions/checkout@v2
          - name: Download artifacts
            uses: actions/download-artifact@v2
            with:
              name: jar-artifacts
              path: app/target
    
          - name: Install Plugin and Deploy
            uses: serverless/github-action@v3.2
            with:
              args: -c "serverless plugin install --name serverless-manifest-plugin && serverless deploy --stage prd"
              entrypoint: /bin/sh
            env:
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        
          - name: manifest
            id: manifest
            run: |
              url=$(cat .serverless/manifest.json | jq -r '.prd.urls.apiGateway')
              echo "::set-output name=url::$url"
    
          - run: echo ${{ steps.manifest.outputs.url }}
          
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions