Public/EB/Get-ATEBEnvironmentResourceList.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
function Get-ATEBEnvironmentResourceList
{
    <#
        .SYNOPSIS
            Gets a list of resources associated Elastic Beanstalk environents.

        .DESCRIPTION
            This command gets essential information about the resources in a beanstalk environment.
            Resource information is retured as an object by default so you can do further processing,
            however addition of -AsText switch instead prints out the information and the command returns nothing

        .PARAMETER EnvironmentId
            ID of an Elastic Beanstalk environment

        .PARAMETER EnvironmentName
            Name of an Elastic Beanstalk environment

        .PARAMETER ApplicationName
            Name of an Elastic Beanstalk application. All environments are returned.

        .PARAMETER AsText
            Print the environment information to the console instead of returning it as an object

        .EXAMPLE
            Get-ATEBEnvironmentResourceList -EnvironmentName production -AsText
            Lists the resources of the given environment to the console.

        .EXAMPLE
            Get-ATEBEnvironmentResourceList -EnvironmentId e-edxny3zkbp -AsText
            Lists the resources of the given environment to the console.

        .EXAMPLE
            Get-ATEBEnvironmentResourceList -ApplicationName MYApplication -AsText
            Lists the resources of all environments in the given EB application to the console.

        .EXAMPLE
            Invoke-ATSSMPowerShellScript -InstanceIds (Get-ATEBEnvironmentResourceList -EnvironmentName production).Instances.InstanceId -AsJson -ScriptBlock { Invoke-RestMethod http://localhost/status | ConvertTo-Json }
            Used in conjunction with Invoke-ATSSMPowerShellScript, send a command to all instances in the given Windows environment.

        .EXAMPLE
            Invoke-ATSSMShellScript -InstanceIds (Get-ATEBEnvironmentResourceList -EnvironmentName production).Instances.InstanceId -CommandText "ls -la /"
            Used in conjunction with Invoke-ATSSMShellScript, send a command to all instances in the given Linux environment.

        .OUTPUTS
            [PSObject[]] Information about each environment returned
            or nothing if -AsText specified.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Name')]
    param
    (
        [Parameter(ParameterSetName = 'Id')]
        [string]$EnvironmentId,

        [Parameter(ParameterSetName = 'Name', Position = 0)]
        [string]$EnvironmentName,

        [Parameter(ParameterSetName = 'App')]
        [string]$ApplicationName,

        [switch]$AsText
    )

    # Pass relevant arguments from function call to Get-EBEnvironment
    $envArgs = @{}

    $PSBoundParameters.Keys |
        Where-Object {
        ('EnvironmentId', 'EnvironmentName', 'ApplicationName') -icontains $_
    } |
        ForEach-Object {
        $envArgs.Add($_, $PSBoundParameters[$_])
    }

    $env = Get-EBEnvironment @envArgs

    if (-not $env)
    {
        Write-Host "Environment not found"
        return
    }

    $allEnvs = $env |
        Where-Object {
            ('Updating', 'Ready') -icontains $_.Status
        } |
        ForEach-Object {

        # Name of stack created by Elastic Beanstalk
        $ebStackName = "awseb-$($_.EnvironmentId)-stack"
        $resources = Get-EBEnvironmentResource -EnvironmentId $_.EnvironmentId

        # Create summary object
        $envData = New-Object PSObject -Property @{
            StackName            = $ebStackName
            ApplicationName      = $_.ApplicationName
            EnviromentName       = $_.EnvironmentName
            EnvironmentId        = $_.EnvironmentId
            RunningVersion       = $_.VersionLabel
            LastUpdated          = $_.DateUpdated
            Health               = $_.Health.Value
            Instances            = New-Object PSObject -Property @{
                InstanceId     = $resources.Instances.Id
                SecurityGroups = @()
            }
            LoadBalancers        = @()
            AutoScalingGroups    = $resources.AutoScalingGroups.Name
            LaunchConfigurations = @()
        }

        # Find instance security groups
        $reservation = Get-EC2Instance -Filter @{
            Name   = 'instance-id'
            Values = $resources.Instances.Id | Select-Object -First 1
        }

        if ($null -ne $reservation)
        {
            $instance = $reservation.Instances | Select-Object -First 1
            $envData.Instances.SecurityGroups = Get-SecurityGroupWithStack -GroupId $instance.SecurityGroups.GroupId
        }

        if (($resources.LoadBalancers | Measure-Object).Count -gt 0)
        {
            # Get load balancer(s) and associated security groups
            $envData.LoadBalancers = $resources.LoadBalancers.Name |
                ForEach-Object {

                try
                {
                    # Try classic load balancer
                    $elb = Get-ELBLoadBalancer -LoadBalancerName $_
                }
                catch
                {
                    # try application load balancer
                    $elb = $(
                        if ($_.Name -ilike 'arn:aws*')
                        {
                            Get-ELB2LoadBalancer -LoadBalancerArn $_
                        }
                        else
                        {
                            Get-ELB2LoadBalancer -Name $_
                        }
                    )
                }

                New-Object PSObject -Property @{
                    Name           = $elb.LoadBalancerName
                    SecurityGroups = Get-SecurityGroupWithStack -GroupId $elb.SecurityGroups
                }
            }
        }

        if (($resources.LaunchConfigurations | Measure-Object).Count -gt 0)
        {
            # Get launch configurations and data of interest
            $envData.LaunchConfigurations = $resources.LaunchConfigurations.Name |
                Foreach-Object {

                $lc = Get-ASLaunchConfiguration -LaunchConfigurationName $_
                New-Object PSObject -Property @{
                    Name         = $lc.LaunchConfigurationName
                    ImageId      = $lc.ImageId
                    InstanceType = $lc.InstanceType
                }
            }
        }

        $envData
    }

    if (-not $AsText)
    {
        return $allEnvs
    }

    $allEnvs |
        ForEach-Object {

        Write-Host "Application : $($_.ApplicationName)"
        Write-Host "Environment : $($_.EnviromentName) ($($_.EnvironmentId))"
        Write-Host "Health : $($_.Health)"
        Write-Host "Running Version : $($_.RunningVersion)"
        Write-Host "Stack Name : $($_.StackName)"
        Write-Host "Last Updated : $($_.LastUpdated.ToString('yyyy-MM-dd HH:mm:ss'))"
        Write-Host "AutoScaling Groups: $($_.AutoScalingGroups -join ', ')"
        Write-Host "Instances :"
        Write-Host " Instance IDs : $($_.Instances.InstanceId -join ', ')"
        Write-Host " Security Groups: $(($_.Instances.SecurityGroups | Foreach-Object { $_.ToString() }) -join ', ')"
        Write-Host "Launch Configurations:"
        $_.LaunchConfigurations |
            ForEach-Object {
            Write-Host " Name : $($_.Name)"
            Write-Host " Image ID : $($_.ImageId)"
            Write-Host " Instance Type: $($_.InstanceType)"
        }
        Write-Host "Load Balancers :"
        $_.LoadBalancers |
            ForEach-Object {
            Write-Host " Name : $($_.Name)"
            Write-Host " Security Groups: $(($_.SecurityGroups | Foreach-Object { $_.ToString() }) -join ', ')"
        }

        Write-Host
    }
}