amazon web services - azure pipeline ec2 inventory creation - Stack Overflow
fetch_ec2_inventory.py
import boto3 import pandas as pd import os from datetime import datetime
def fetch_instances(region, profile, stage): """ Fetch EC2 instances from a specific AWS account, region, and stage. """ session = boto3.Session(profile_name=profile, region_name=region) ec2 = session.client('ec2') response = ec2.describe_instances() instances = [] for reservation in response['Reservations']: for instance in reservation['Instances']: name_tag = next((tag['Value'] for tag in instance.get('Tags', []) if tag['Key'] == 'Name'), None) if name_tag and stage in name_tag: # Filter by stage in instance name instances.append({ "Account": profile, "Region": region, "InstanceId": instance['InstanceId'], "InstanceType": instance['InstanceType'], "State": instance['State']['Name'], "LaunchTime": instance['LaunchTime'].strftime("%Y-%m-%d %H:%M:%S"), "Name": name_tag }) return instances
def save_to_csv(data, stage): """ Save EC2 instance data to a consolidated CSV file for the given stage. """ timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") filename = f"ec2_inventory_{stage}_{timestamp}.csv" df = pd.DataFrame(data) df.to_csv(filename, index=False) print(f"Inventory saved to {filename}") return filename
def main(): """ Main function to fetch EC2 inventory based on provided environment variables. """ profile = os.getenv('AWS_PROFILE') regions = os.getenv('AWS_REGIONS', 'us-east-1').split(',') stage = os.getenv('STAGE')
if not profile or not stage:
raise ValueError("AWS_PROFILE and STAGE environment variables must be set.")
all_instances = []
for region in regions:
print(f"Fetching EC2 instances for Account: {profile}, Region: {region}, Stage: {stage}")
instances = fetch_instances(region, profile, stage)
all_instances.extend(instances)
if all_instances:
save_to_csv(all_instances, stage)
else:
print(f"No instances found for stage '{stage}'.")
if name == "main": main()
azure
parameters:
name: stage type: string default: 'dev' displayName: "Stage (dev, uat, prod)"
name: awsProfile type: string default: '' displayName: "AWS Account Profile"
name: awsRegions type: string default: 'us-east-1,us-west-2' displayName: "AWS Regions (comma-separated)"
trigger:
- main
pool: vmImage: 'ubuntu-latest'
stages:
- stage: EC2Inventory
displayName: "Generate EC2 Inventory"
jobs:
- job: InventoryJob
displayName: "Run EC2 Inventory Script"
steps:
task: UsePythonVersion@0 inputs: versionSpec: '3.x' addToPath: true
script: | pip install boto3 pandas displayName: "Install Python Dependencies"
script: | python scripts/fetch_ec2_inventory.py env: AWS_PROFILE: ${{ parameters.awsProfile }} AWS_REGIONS: ${{ parameters.awsRegions }} STAGE: ${{ parameters.stage }} displayName: "Run EC2 Inventory Script"
task: PublishBuildArtifacts@1 inputs: pathToPublish: "*.csv" artifactName: "ec2-inventory-${{ parameters.stage }}" publishLocation: "Container" displayName: "Publish EC2 Inventory for ${{ parameters.stage }}"
- job: InventoryJob
displayName: "Run EC2 Inventory Script"
steps:
- 你知道多少?细数五花八门的生物识别技术
- 黑客称用商业软件复制出德防长指纹
- Mac电脑与PC九大区别
- 力压谷歌、苹果,微软凭什么成为软件霸主?
- 谷歌眼镜计划明年“高价”上市(组图)
- How can I design a button with two colors and a curved border in CSS? - Stack Overflow
- Google Translate Widget Only Works in Chrome – Why? - Stack Overflow
- dataframe - Dataset uploaded and verified, but cannot use read.csv - Stack Overflow
- javascript - How to delete content from content-editable field on facebookmessenger.com? - Stack Overflow
- I want to run simple php with frankenphp inside docker - Stack Overflow
- Teradata: How can I trim a column for leading zeros and trailing spaces? - Stack Overflow
- java - Bluej throws SSLHandshakeException making http request - Stack Overflow
- c# - System.Printing.PrintJobException: 'An exception occurred while setting the print job. Win32 error: The parameter i
- reactjs - How to import svg icons in Nextjs 15 - Stack Overflow
- javascript - Why does every object in BabylonJS require a name? - Stack Overflow
- jhipster - Jhispter cannot run - Stack Overflow
- reactjs - Display SVG string in the React frontend - Stack Overflow