LoginSignup
0
0

More than 5 years have passed since last update.

awspecでEC2のIAM Roleをテストする

Posted at

awspecでEC2に割り当てられているIAM Roleをテストしたいときの話です。

ここを見ると、its(:iam_instance_profile)というのがあります。例えばEC2-Roleというロールが割り当てられるていることをテストしたいとき、これを使って、

describe ec2('instance_name') do
  its(:iam_instance_profile) { should match /EC2-Role$/ }
end

とやると、

  1) ec2 'instance_name' iam_instance_profile should match /EC2-Role$/
     Failure/Error: its(:iam_instance_profile) { should match /EC2-Role$/ }

       expected #<struct Aws::EC2::Types::IamInstanceProfile arn="arn:aws:iam::xxxxxxxxxxx:instance-profile/EC2-Role", id="AIPAXXXXXXXXXXXXXX"> to match /EC2-Role$/
       Diff:
       @@ -1,2 +1,2 @@
       -/EC2-Role$/
       +"#<struct Aws::EC2::Types::IamInstanceProfile arn=\"arn:aws:iam::xxxxxxxxxxxxx:instance-profile/EC2-Role\", id=\"AIPAXXXXXXXXXXXXXX\">"

こんなエラーになり正しく動きません。
its(:iam_instance_profile)Aws::EC2::Types::IamInstanceProfileを返すみたいなので、そのまま文字列比較はできないようです。

正しい書き方は分かりませんが、ここを見ると、Aws::EC2::Instanceのリソースが使えるとのことなので、

describe ec2('instance_name') do
  its('iam_instance_profile.arn') { should match /EC2-Role$/ }
end

と書けばテストできました。

ec2 'instance_name'
  iam_instance_profile.arn
    should match /EC2-Role$/
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0