Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions public_cloud/query_and_stop_aws_ec2_instances_by_tag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Query and Stop AWS EC2 Instances by Tag

Query a list of running AWS EC2 instances with the tag key-value pair `status: decom` using Steampipe and then stop them.

## Installation

Download and install Flowpipe (https://flowpipe.io/downloads). Or use Brew:

```sh
brew tap turbot/tap
brew install flowpipe
```

Clone:

```sh
git clone https://github.com/turbot/flowpipe-samples.git
cd public_cloud/query_and_stop_aws_ec2_instance
```

[Install mod dependencies](https://flowpipe.io/docs/build/mod-dependencies#mod-dependencies):

```sh
flowpipe mod install
```

## Credentials

By default, the following environment variables will be used for authentication:

- `AWS_PROFILE`
- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`
- `AWS_PROFILE`

You can also create `credential` resources in configuration files:

```sh
vi creds.fpc
```

```hcl
credential "aws" "aws_profile" {
profile = "my-profile"
}

credential "aws" "aws_access_key_pair" {
access_key = "AKIA..."
secret_key = "dP+C+J..."
}

credential "aws" "aws_session_token" {
access_key = "AKIA..."
secret_key = "dP+C+J..."
session_token = "AQoDX..."
}
```

For more information on credentials in Flowpipe, please see [Managing Credentials](https://flowpipe.io/docs/run/credentials).

## Usage

Run the pipeline and specify the `aws_region` pipeline arguments:

```sh
flowpipe pipeline run query_and_stop_aws_ec2_instance --arg aws_region=us-east-1
```

## Configuration

To avoid entering variable values when running the pipeline or starting the server, you can set variable values:

```sh
cp flowpipe.fpvars.example flowpipe.fpvars
vi flowpipe.fpvars
```

```hcl
# Optional
# aws_cred = "non_default_cred"
```

## Open Source & Contributing

This repository is published under the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0). Please see our [code of conduct](https://github.com/turbot/.github/blob/main/CODE_OF_CONDUCT.md). We look forward to collaborating with you!

[Flowpipe](https://flowpipe.io) is a product produced from this open source software, exclusively by [Turbot HQ, Inc](https://turbot.com). It is distributed under our commercial terms. Others are allowed to make their own distribution of the software, but cannot use any of the Turbot trademarks, cloud services, etc. You can learn more in our [Open Source FAQ](https://turbot.com/open-source).

## Get Involved

**[Join #flowpipe on Slack →](https://flowpipe.io/community/join)**

Want to help but not sure where to start? Pick up one of the `help wanted` issues:

- [Flowpipe](https://github.com/turbot/flowpipe/labels/help%20wanted)
- [Samples Mod](https://github.com/turbot/flowpipe-samples/labels/help%20wanted)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Required
aws_region = "us-east-1"

# Optional
# aws_cred = "non_default_cred"
12 changes: 12 additions & 0 deletions public_cloud/query_and_stop_aws_ec2_instances_by_tag/mod.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod "query_and_stop_aws_ec2_instances_by_tag" {
title = "Query and Stop AWS EC2 Instances by Tag"
description = "Query and stop AWS EC2 instance based on the `status` tag value."
documentation = file("./README.md")
categories = ["public cloud"]

require {
mod "github.com/turbot/flowpipe-mod-aws" {
version = "0.1.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pipeline "query_and_stop_aws_ec2_instances_by_tag" {
title = "Query and Stop AWS EC2 Instances by Tag"
description = "Query and stop AWS EC2 instance based on the `status` tag value."

param "aws_region" {
type = string
description = "The name of the Region."
default = var.aws_region
}

param "aws_cred" {
type = string
description = "Name for AWS credential to use. If not provided, the default credential will be used."
default = var.aws_cred
}

step "query" "list_ec2_instances" {
connection_string = "postgres://steampipe@localhost:9193/steampipe"
sql = <<-EOQ
select
instance_id
from
aws_ec2_instance
where
instance_state = 'running'
and tags ->> 'status' = 'decom'
EOQ
}

step "pipeline" "stop_aws_ec2_instances" {
for_each = step.query.list_ec2_instances.rows
pipeline = aws.pipeline.stop_ec2_instances
args = {
region = param.aws_region
cred = param.aws_cred
instance_ids = ["${each.value.instance_id}"]
}
}

output "stopped_instances" {
description = "Stopped instances."
value = join(", ", [for instance in values(step.pipeline.stop_aws_ec2_instances) : instance.output.instances[0].InstanceId])
}
}
10 changes: 10 additions & 0 deletions public_cloud/query_and_stop_aws_ec2_instances_by_tag/variables.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "aws_region" {
type = string
description = "The name of the Region."
}

variable "aws_cred" {
type = string
description = "Name for AWS credentials to use. If not provided, the default credentials will be used."
default = "default"
}