Skip to content

Instantly share code, notes, and snippets.

@Sigler
Last active August 3, 2021 17:32
Show Gist options
  • Save Sigler/6ea9fb12e79a760bfc0611f4df200f08 to your computer and use it in GitHub Desktop.
Save Sigler/6ea9fb12e79a760bfc0611f4df200f08 to your computer and use it in GitHub Desktop.

Revisions

  1. Sigler revised this gist Aug 3, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    summary: Remove unused EC2 key pairs

    workflow-loop:
    # only showing 5 of 500
    - set: prod
    parameters:
    - aws-connection: aws-prod-cac
    @@ -14,6 +13,7 @@ workflow-loop:
    aws-awsRegion: eu-west-2
    - aws-connection: aws-prod-apne
    awsRegion: ap-northeast-2
    # only showing 5 of 500

    - set: dev
    - connection: aws-dev-foo
  2. Sigler revised this gist Aug 3, 2021. 1 changed file with 19 additions and 22 deletions.
    41 changes: 19 additions & 22 deletions remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -3,19 +3,17 @@ summary: Remove unused EC2 key pairs
    workflow-loop:
    # only showing 5 of 500
    - set: prod
    variables:
    awsRegion: us-east-1
    parameters:
    - connection: aws-prod-cac
    awsRegion: {{awsRegion}}
    - connection: aws-prod-euc
    awsRegion: {{awsRegion}}
    - connection: aws-prod-euw1
    awsRegion: {{awsRegion}}
    - connection: aws-prod-euw2
    awsRegion: {{awsRegion}}
    - connection: aws-prod-apne
    awsRegion: {{awsRegion}}
    - aws-connection: aws-prod-cac
    awsRegion: ca-central-1
    - aws-connection: aws-prod-euc
    awsRegion: eu-central-1
    - aws-connection: aws-prod-euw1
    awsRegion: eu-west-1
    - aws-connection: aws-prod-euw2
    aws-awsRegion: eu-west-2
    - aws-connection: aws-prod-apne
    awsRegion: ap-northeast-2

    - set: dev
    - connection: aws-dev-foo
    @@ -24,9 +22,9 @@ workflow-loop:
    awsRegion: us-east-1

    parameters:
    connection:
    aws-connection:
    description: The target account
    default:
    default: my-aws-account
    awsRegion:
    description: The AWS region to run in
    default: us-east-1
    @@ -41,23 +39,22 @@ triggers:
    schedule: '0 * * * *'
    binding:
    parameters:
    awsRegion: {{awsRegion}}
    awsRegion: us-east-1
    dryRun: true

    steps:
    - name: describe-instances
    image: relaysh/aws-ec2-step-instances-describe
    spec:
    aws:
    connection: ${parameters.connection}
    # connection: ${connections.aws.{{my-aws-account}}}
    region: ${parameters.{{awsRegion}}}
    connection: ${parameters.aws-connection}
    region: ${parameters.awsRegion}

    - name: describe-key-pairs
    image: relaysh/aws-ec2-step-key-pairs-describe
    spec:
    connection: ${connections.aws.{{my-aws-account}}}
    region: ${parameters.{{awsRegion}}}
    connection: ${parameters.aws-connection}
    region: ${parameters.awsRegion}

    - name: filter-key-pairs
    image: relaysh/core:latest-python
    @@ -79,6 +76,6 @@ steps:
    spec:
    aws:
    # This uses a separate connection named awsAdmin. What kind of issues does this surface?
    connection: ${connections.aws.awsAdmin}
    region: ${parameters.{{awsRegion}}}
    connection: ${parameters.aws-connection}
    region: ${parameters.awsRegion}
    keyPairNames: !Output {from: filter-key-pairs, name: keyPairNames}
  3. Sigler revised this gist Aug 2, 2021. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -3,16 +3,19 @@ summary: Remove unused EC2 key pairs
    workflow-loop:
    # only showing 5 of 500
    - set: prod
    variables:
    awsRegion: us-east-1
    parameters:
    - connection: aws-prod-cac
    awsRegion: ca-central-1
    awsRegion: {{awsRegion}}
    - connection: aws-prod-euc
    awsRegion: eu-central-2
    awsRegion: {{awsRegion}}
    - connection: aws-prod-euw1
    awsRegion: eu-west-1
    awsRegion: {{awsRegion}}
    - connection: aws-prod-euw2
    awsRegion: eu-west-2
    awsRegion: {{awsRegion}}
    - connection: aws-prod-apne
    awsRegion: ap-northeast-1
    awsRegion: {{awsRegion}}

    - set: dev
    - connection: aws-dev-foo
    @@ -21,6 +24,9 @@ workflow-loop:
    awsRegion: us-east-1

    parameters:
    connection:
    description: The target account
    default:
    awsRegion:
    description: The AWS region to run in
    default: us-east-1
    @@ -43,7 +49,8 @@ steps:
    image: relaysh/aws-ec2-step-instances-describe
    spec:
    aws:
    connection: ${connections.aws.{{my-aws-account}}}
    connection: ${parameters.connection}
    # connection: ${connections.aws.{{my-aws-account}}}
    region: ${parameters.{{awsRegion}}}

    - name: describe-key-pairs
  4. Sigler revised this gist Aug 2, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ workflow-loop:
    - connection: aws-prod-cac
    awsRegion: ca-central-1
    - connection: aws-prod-euc
    awsRegion: eu-central-dos
    awsRegion: eu-central-2
    - connection: aws-prod-euw1
    awsRegion: eu-west-1
    - connection: aws-prod-euw2
  5. Sigler revised this gist Aug 2, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -71,6 +71,7 @@ steps:
    when: ${parameters.dryrun == 'false'}
    spec:
    aws:
    # This uses a separate connection named awsAdmin. What kind of issues does this surface?
    connection: ${connections.aws.awsAdmin}
    region: ${parameters.{{awsRegion}}}
    keyPairNames: !Output {from: filter-key-pairs, name: keyPairNames}
  6. Sigler revised this gist Aug 2, 2021. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    summary: Remove unused EC2 key pairs

    workflow-loop:
    # only showing 5 of 500
    - set: prod
    - connection: aws-prod-cac
    awsRegion: ca-central-1
    @@ -17,7 +18,7 @@ workflow-loop:
    - connection: aws-dev-foo
    awsRegion: us-west-1
    - connection: aws-dev-bar
    awsRegion: us-west-1
    awsRegion: us-east-1

    parameters:
    awsRegion:
    @@ -70,6 +71,6 @@ steps:
    when: ${parameters.dryrun == 'false'}
    spec:
    aws:
    connection: ${connections.aws.{{my-aws-account}}}
    connection: ${connections.aws.awsAdmin}
    region: ${parameters.{{awsRegion}}}
    keyPairNames: !Output {from: filter-key-pairs, name: keyPairNames}
  7. Sigler created this gist Aug 2, 2021.
    75 changes: 75 additions & 0 deletions remove-unused-keypairs-loop.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    summary: Remove unused EC2 key pairs

    workflow-loop:
    - set: prod
    - connection: aws-prod-cac
    awsRegion: ca-central-1
    - connection: aws-prod-euc
    awsRegion: eu-central-dos
    - connection: aws-prod-euw1
    awsRegion: eu-west-1
    - connection: aws-prod-euw2
    awsRegion: eu-west-2
    - connection: aws-prod-apne
    awsRegion: ap-northeast-1

    - set: dev
    - connection: aws-dev-foo
    awsRegion: us-west-1
    - connection: aws-dev-bar
    awsRegion: us-west-1

    parameters:
    awsRegion:
    description: The AWS region to run in
    default: us-east-1
    dryRun:
    description: True if you dont want to actually delete the resources. Use this to test the workflow and ensure it is behaving as expected.
    default: 'true'

    triggers:
    - name: schedule
    source:
    type: schedule
    schedule: '0 * * * *'
    binding:
    parameters:
    awsRegion: {{awsRegion}}
    dryRun: true

    steps:
    - name: describe-instances
    image: relaysh/aws-ec2-step-instances-describe
    spec:
    aws:
    connection: ${connections.aws.{{my-aws-account}}}
    region: ${parameters.{{awsRegion}}}

    - name: describe-key-pairs
    image: relaysh/aws-ec2-step-key-pairs-describe
    spec:
    connection: ${connections.aws.{{my-aws-account}}}
    region: ${parameters.{{awsRegion}}}

    - name: filter-key-pairs
    image: relaysh/core:latest-python
    spec:
    instances: !Output {from: describe-instances, name: instances}
    keyPairs: !Output { from: describe-key-pairs, name: keyPairs}
    inputFile: https://raw.githubusercontent.com/puppetlabs/relay-workflows/master/ec2-remove-unused-key-pairs/filter-key-pairs.py

    - name: approval
    description: Wait for approval to delete key pairs
    type: approval
    dependsOn: filter-key-pairs
    when: ${parameters.dryrun == 'false'}

    - name: delete-key-pairs
    dependsOn: approval
    image: relaysh/aws-ec2-step-key-pairs-delete
    when: ${parameters.dryrun == 'false'}
    spec:
    aws:
    connection: ${connections.aws.{{my-aws-account}}}
    region: ${parameters.{{awsRegion}}}
    keyPairNames: !Output {from: filter-key-pairs, name: keyPairNames}