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:
- 微软要逆天!SurfacePhone能运行exe程序
- 微软反攻打响跨界战争:Win8移动领域伸橄榄枝
- javascript - Read text file and put result to innerHTML? - Stack Overflow
- python - How to Get a Single Solution from a Physics-Informed Neural Network (PINN) for a PDE with Initial and Boundary Conditio
- Here Attributes API - Unable to paginate response data - Stack Overflow
- android - Not receiving events using a BroadcastReceiver - Stack Overflow
- python - How to sub-class LangGraph's MessageState or use Pydantic for channel separation - Stack Overflow
- c# - Is there a way to fill scriptable object field with a child class? - Stack Overflow
- c# - System.Printing.PrintJobException: 'An exception occurred while setting the print job. Win32 error: The parameter i
- using Okta principal groups and metadata in Spring Boot with Okta Spring Boot - Stack Overflow
- java - XSLT 3.0 chained burst-streaming with saxon - memory consumption considerations - Stack Overflow
- flash - Movie clip loading display problem in ActionScript 2 - Stack Overflow
- python - mecab-python3 AWS lambda "circular import" error - Stack Overflow
- postgresql - AzureStorageClient raises the error Unable to determine account name for shared key credential - Stack Overflow
- javascript - What are the benefits of Next.js Server Actions, and why should I use them instead of client-side API calls? - Stac
- GHC unable to find mingwinclude directory on Windows - Stack Overflow
- swift - Popovers not displayed inside ForEach over Enum types - Stack Overflow