sorta kinda...

主にAWS関連ですが、これに限らずいろいろ勉強したことや思ったことを書いていきます。

新しい Windows Server AMI のリリース通知を受け取ろう

ナスです。

仕事でいろいろ調べてたら、新しい Windows Server AMI がリリースされたら通知してくれる SNS があることを知りました。
docs.aws.amazon.com

どんなもんか知りたくなったので試してみました。

 

サブスクリプションを作成するだけ

手順は簡単で、上のリンクのまんまです。せっかくなので aws cli でやってみます。

aws --region us-east-1 sns subscribe --topic-arn arn:aws:sns:us-east-1:801119661308:ec2-windows-ami-update --protocol email --notification-endpoint xxx@domain.com
{
    "SubscriptionArn": "pending confirmation"
}

こんな感じで実行すると、一番後ろで指定したメールアドレスに確認メールが飛んできます。メール本文にある Confirm subscription のリンクをクリックすれば OK です。これで新しい Windows Server AMI がリリースされたらメールが飛んでくるようになります。

 

どんな内容の通知がくるのか?

サブスクリプション作成した翌日に奇跡的に通知が来たので、その内容を貼ります。

A new version of Amazon Machine Images has been released. Previous versions of Amazon published Windows AMIs 2018.02.30 and older will be deprecated on Tuesday, May 1st, at 10AM Pacific.
Please use this time to update any dependencies such as CloudFormation or Autoscaling groups.
 
EC2 Quickstart, AWS MarketPlace and CloudFormation templates will be updated over the next couple days.
 
New AMIs are dated 2018.04.11.
Search for this using the AWS Console, PowerShell Get-EC2ImageByName or using Powershell Get-SSMParametersByPath -Path "/aws/service/ami-windows-latest"
 
Changes:
- Microsoft Security Updates current to April 10th 2018
- Amazon SSM Agent v2.2.392.0
- Amazon EC2Config v4.9.2586
- AWS Tools for Windows PowerShell v3.3.256.0
- AWS CloudFormation v1.4.30
- Serial INF and Intel Chipset INF configurations included to support new EC2 instance types
- SQL Server 2017 AMIs updated to CU5
- SQL Server 2016 SP1 AMIs updated to CU8
 
Important:
Microsoft ended mainstream support for Windows Server 2016 Datacenter and Standard editions for Nano Server installation options as of 4/10/2018. (See the Microsoft Support Lifecycle page for more details: https://support.microsoft.com/en-us/lifecycle/search?alpha=nano)
A final update for Windows_Server-2016-English-Nano-Base AMI is in the April Windows AMI release. Access to all public versions of Windows_Server-2016-English-Nano-Base will be removed in September 2018.
Additional information about Nano Server lifecycle including details on launching Nano Server as a Container can be found here: https://docs.microsoft.com/en-us/windows-server/get-started/nano-in-semi-annual-channel
以下省略…

 

通知がいらなくなったら

サブスクリプションの解除をすれば OK です。サブスクリプション ARN を指定しないといけないので、こんな感じで ARN を確認します。

aws sns list-subscriptions --region us-east-1 | jq '.Subscriptions[] | select(.TopicArn=="arn:aws:sns:us-east-1:801119661308:ec2-windows-ami-update")'
{
  "SubscriptionArn": "arn:aws:sns:us-east-1:801119661308:ec2-windows-ami-update:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "Protocol": "email",
  "TopicArn": "arn:aws:sns:us-east-1:801119661308:ec2-windows-ami-update",
  "Endpoint": "xxx@domain.com",
  "Owner": "123456789012"
}

あとは確認した ARN を使って解除するコマンドを実行するだけです。↓は、ARN の確認と同時にサブスクリプション解除するワンライナーです。

ARN=`aws sns list-subscriptions --region us-east-1 | jq -r '.Subscriptions[] | select(.TopicArn=="arn:aws:sns:us-east-1:801119661308:ec2-windows-ami-update") | .SubscriptionArn'` && aws --region us-east-1 sns unsubscribe --subscription-arn $ARN

 

むちゃくちゃ簡単ですね。SNS での通知なので、メールじゃなくて slack に通知したり、SNS をトリガーにして lambda で何か処理したり、といったこともできそうです。

EC2 インスタンスにつけた IAM ロールが反映されるまで

ナスです。

今日は IAM ロールに小一時間ほど悩まされたので、今度こそ忘れずに書いておきます。今度こそと書いたのは、過去に似たような内容の記事を書いていたのに完全に忘れていたからです…

 

IAM ロールが効いていない?

EC2 インスタンススクリプトを置いてそこから実行できるように IAM ロールを付けたんですが、いつまで経っても AWS CLI が実行できなかったんです。テストで下記コマンドを試したらこうなりました。

[ec2-user@ip-172-31-43-xxx ~]$ aws sts get-caller-identity

'AccessKeyId'
[ec2-user@ip-172-31-43-xxx~]$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/admin_role
{
  "Code" : "AssumeRoleUnauthorizedAccess",
  "Message" : "EC2 cannot assume the role admin_role.  Please see documentation at http://docs.amazonwebservices.com/IAM/latest/UserGuide/RolesTroubleshooting.html.",
  "LastUpdated" : "2018-04-10T02:48:40Z"
}

ん? IAM ロールはついてるし権限もちゃんとつけてるしなんで??って思っていろいろ調べてからまた同じコマンドを実行してもダメで

[ec2-user@ip-172-31-43-xxx~]$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/admin_role
{
  "Code" : "AssumeRoleUnauthorizedAccess",
  "Message" : "EC2 cannot assume the role admin_role.  Please see documentation at http://docs.amazonwebservices.com/IAM/latest/UserGuide/RolesTroubleshooting.html.",
  "LastUpdated" : "2018-04-10T02:56:57Z"
}

この時点で 2 回のコマンド実行の間隔は 8 分経過でした。

 

あ!再起動すればいいのか?

昔は起動済みのインスタンスには IAM ロールをつけられなかったので、再起動すればいいかも!って思って再起動したらうまくいきました。

[ec2-user@ip-172-31-43-xxx~]$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/admin_role
{
  "Code" : "Success",
  "LastUpdated" : "2018-04-10T02:58:04Z",
  "Type" : "AWS-HMAC",
  "AccessKeyId" : "AAAAAAAAAAAAAAAAAAAA",
  "SecretAccessKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "Token" : "ZZZZZZZZZ...長いので省略",
  "Expiration" : "2018-04-10T09:27:01Z"
}
[ec2-user@ip-172-31-43-xxx~]$ aws sts get-caller-identity
{
    "Account": "123456789012",
    "UserId": "AAAAAAAAAAAAAAAAAA:i-1234567890abcdefg",
    "Arn": "arn:aws:sts::123456789012:assumed-role/admin_role/i-1234567890abcdefg"
}
[ec2-user@ip-172-31-43-xxx~]$ aws s3 ls
2018-04-10 02:17:01 nasutest1

 

同じ事例ないか調べてたら…

ありました。自分で書いた記事が…
nasrinjp1.hatenablog.com

この時は Linux で 5 分程度待てば IAM ロールが反映されたみたいですが、今回は 8 分待ってもダメだったので、OS はあまり関係なく、AWS 側で定期的にジョブか何かでチェックしてるんですかねー

というわけで、今回の件で IAM ロールはすぐには反映されないことを覚えた(はず…