New-Object : Cannot find type [Amazon.EC2.Model.Tag]

Viewed 1563

I am using New-EC2Tag to create/update the EC2 tags through Azure Pipeline. I am using "AWS Tools for Windows PowerShell Script" task and below is the code:

$Tag = New-Object Amazon.EC2.Model.Tag  
$Tag.Key = "DesiredInstanceState"  
$Tag.Value = "Stopped"

New-EC2Tag -Resource $instanceName -Tag $Tag

When the task runs I get below error:

2020-06-16T18:40:57.4642775Z ##[error]New-Object : Cannot find type [Amazon.EC2.Model.Tag]: verify that the assembly containing this type is loaded. At C:\1_work_temp\d2378116-3224-4a4a-b92c-61744a291aac.ps1:2 char:8 + $Tag = New-Object Amazon.EC2.Model.Tag + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand 2020-06-16T18:40:57.4655375Z ##[error]PowerShell exited with code '1'.

I installed the module on both the build server and the server for which I am trying to create the tag, still the same.

Any input would be greatly appreciated. Thanks!

2 Answers

As suggested by @Mathias in the comments, I updated the script to include Import-Module -name AWSPowerShell and it worked like a charm.

I had an issue with creating DateInterval for AWS costexplorer, tried:

Install-Module -Name AWSPowerShell -force -SkipPublisherCheck

Import-Module -Name AWSPowerShell

Works as expected.

Related