Bootstrap FreeKB - GitHub Actions - Get EC2 Instance public DNS name
GitHub Actions - Get EC2 Instance public DNS name

Updated:   |  GitHub Actions articles

GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.

For example, perhaps you are using appleboy/scp-action@master to SCP one or more files in one of your GitHub repositories to one or more target servers to one of your Amazon Web Services (AWS) EC2 instances. Notice in this example that the DNS name of the EC2 instance is used (ec2-107-21-28-51.compute-1.amazonaws.com in this example).

name: GitHub Action
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  push:
    branches:
      - main
jobs:
  github-action-job:
    runs-on: ubuntu-latest
    steps:      
      - name: Checking out the repository code
        uses: actions/checkout@v4
      
      - name: copy foo.jpg to /tmp
        uses: appleboy/scp-action@master
        with:
          host: ec2-107-21-28-51.compute-1.amazonaws.com
          username: ec2-user
          password: ${{ secrets.PASSWORD}}
          port: 22
          source: "foo.jpg"
          target: "/tmp" 

 

But what if the EC2 instances DNS name changes? Of course, you could then update your GitHub Action to have the new DNS name. I was originally using truemark/aws-ec2-describe-instance-action to get my EC2 instances DNS name, but due to the following issue, I stopped using truemark/aws-ec2-describe-instance-action and instead, am using a simple run action.

 

AVOID TROUBLE

With truemark/aws-ec2-describe-instance-action@v2 (version 2) I was getting the following

Node.js 16 actions are deprecated. Please update the following actions to use Node.js 20: truemark/aws-ec2-describe-instance-action@v2. For more information see: https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/.

Unable to resolve action truemark/aws-ec2-describe-instance-action@v3, unable to find version v3``

I opened this issue in March of 2024 - https://github.com/truemark/aws-ec2-describe-instance-action/issues/93

I was worried that I was going to run into issues due to the deprecation warning, so I replaced  truemark/aws-ec2-describe-instance-action with a run action, which isn't as "clean" looking but at least I no longer get the deprecation warnings

name: GitHub Action
run-name:  ${{ github.workflow }} run by ${{ github.actor }}
on:
  push:
    branches:
      - main
jobs:
  github-action-job:
    runs-on: ubuntu-latest
    steps:      
      - name: Checking out the repository code
        uses: actions/checkout@v4
      
      #- name: describe my-ec2-instance
      #  id: my-ec2-instance
      #  uses: truemark/aws-ec2-describe-instance-action@v2
      #  with:
      #    instance-id: i-123abc456def789gh
      #    region: us-east-1

      #- name: echo my-ec2-instancepublic-dns-name
      #  run: |
      #    echo ${{ steps.my-ec2-instance.outputs.public-dns-name }}

      - name: get my_instance_PublicDnsName
        id: my_instance_ip
        run: my_instance_PublicDnsName=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=my-instance" | jq ".Reservations[].Instances[].PublicDnsName" | sed 's|"||g'); echo "my_instance_PublicDnsName=$my_instance_PublicDnsName" >> $GITHUB_OUTPUT

      - name: echo my_instance_PublicDnsName
        run: |
          echo ${{ steps.my_instance_ip.outputs.my_instance_PublicDnsName }}   

      - name: copy foo.jpg to /tmp
        uses: appleboy/scp-action@master
        with:
          host: ${{ steps.my_instance_ip.outputs.my_instance_PublicDnsName }}
          username: ec2-user
          password: ${{ secrets.PASSWORD}}
          port: 22
          source: "foo.jpg"
          target: "/tmp" 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 848849 in the box below so that we can be sure you are a human.