LoginSignup
0
0

More than 3 years have passed since last update.

The security token included in the request is expiredとなった時の対応方法

Last updated at Posted at 2020-11-24

事象 : Secrets Managerからシークレット値を取得しようとしたら怒られた

  • 環境
    • AWS Cloud9
    • Python 3.6.12

はじめてSecrets Managerにシークレット値を設定してCloud9からシークレット値を取得しようとした。
取得するコードは超親切にシークレット値を設定した時に表示されるのでほぼコピペで使った・・・がExpiredTokenException・・・

def get_backlog_api_key():

    secret_name = 'シークレットの名前'

    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name='リージョン'
    )

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        print(e.response['Error'])
        raise e
    else:
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
{'Message': 'The security token included in the request is expired', 'Code': 'ExpiredTokenException'}

原因 : Lambdaに権限がないから

権限の存在を忘却していた。

Minimum permissions
To run this command, you must have the following permissions:
secretsmanager:GetSecretValue
GetSecretValue - AWS Secrets Manager

対応 : 権限を付与する

  1. AWSのコンソール > Lambda > 対象の関数 > [アクセス権限]タブ > [実行ロール]からIAMの画面を開く
  2. [アクセス権限]タブ > [ポリシーをアタッチします]ボタン
  3. secretsmanagerを検索 > SecretsManagerReadWriteにチェック > [ポリシーのアタッチ]ボタンで権限追加
    • ちょっと雑、取得するだけならもっと権限の範囲は小さいほうがいいけど今はとりあえず
    • image.png
  4. Cloud9のLambdaマークから更新ボタンで更新
    • image.png
    • 更新しても :arrow_double_down: こうなっている間はうまくいかない。なんでこれが出るのかよくわからない、しばらくしてから更新すると消える。よくわからない。
      • image.png
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