amazon web services - azure pipeline ec2 inventory creation - Stack Overflow

时间: 2025-01-06 admin 业界

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 }}"

最新文章