LoginSignup
0
0

More than 1 year has passed since last update.

Workload Identity連携で、GCP操作するLambdaをAWS SAMで作成

Posted at

はじめに

AWSからGCPの操作がセキュアにできるWorkload Identity連携を、AWS SAMを使って作成したLambdaから行った内容を記事にしました。

やったこと

GCP側

以前の記事をそのままやっています。

こちらで作られたファイルをそのまま使います。

AWS側

Cloud9

環境

AWS SAMを実行するので、ボリュームは50GB程度あるといいです。
以前記事にした内容と同じ環境でやっています。

設定

ローカルでテストを行う際に、Cloud9からGCPにアクセスできるよう、EC2にIAMロールを付けます。
前回の記事と同様です。

注意ですが、このIAMロールのままデプロイするのであれば、その権限も付与しておく必要があります。

フォルダ構成・ファイル

Clooud9上にフォルダやファイルを配置していきます。

sample-a2g
├── functions
│   ├── clientLibraryConfig-awstogcp.json
│   ├── function.py
│   └── requirements.txt
└── template.yaml
  • clientLibraryConfig-awstogcp.jsonはGCPで作った認証ファイルです
functions/function.py
from google.cloud import storage

def lambda_handler(event, context):

  storage_client = storage.Client()
  buckets = list(storage_client.list_buckets())
  print(buckets)
  return {
      'statusCode': 200
  }

用いるライブラリはrequirements.txtに記載します。

functions/requirements.txt
google-cloud-storage

テンプレートファイルは以下です。Lambdaが書き込むCloudWatchロググループも一緒に作っています

template.yaml
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for Lambda Function

Parameters:
    LambdaFunctionName:
        Type: String
        Default: "testfunctiona2g"

Resources:
  FunctionRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "for-lambdafunction-${LambdaFunctionName}"
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: '/service-role/'
      Policies:
        # CloudWatch
        - PolicyName: write-cloudwatchlogs
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action: 
                  - 'logs:CreateLogStream'
                  - 'logs:PutLogEvents'
                Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*"

  FunctionLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${LambdaFunctionName}
      RetentionInDays: 3653

  TargetFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Ref LambdaFunctionName
      Role: !GetAtt FunctionRole.Arn
      CodeUri: functions/
      Handler: function.lambda_handler
      Runtime: python3.9
      Environment:
        Variables:
          GOOGLE_APPLICATION_CREDENTIALS: "clientLibraryConfig-awstogcp.json"
          GCLOUD_PROJECT: "hogehoge-pj"
  • LambdaのIAMロールは、GCPが実行できるものにしてください
    • 今回は、AWSの全てのIAMロールを実行できるようにしているので、任意のIAMロールが付いていればOKになります
  • GCPに接続するために必要な環境変数は最後の4行で設定しています。

コードから直接実行

以前の記事と同様にlaunch.jsonを編集します。

実行する際の設定は以下になります。

launch.json
{
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "test-a2g",
            "invokeTarget": {
                "target": "code",
                "projectRoot": "sample-a2g/functions",
                "lambdaHandler": "function.lambda_handler"
            },
            "lambda": {
                "runtime": "python3.9",
                "payload": {},
                "environmentVariables": {
                    "GOOGLE_APPLICATION_CREDENTIALS": "clientLibraryConfig-awstogcp.json",
                    "GCLOUD_PROJECT": "hogehoge-pj"
                }
            }
        }
    ]
}
  • 環境変数はlambda - environmentVariablesの中に記述します。

実行し、成功するとコンソールにバケット一覧が出力されます。
image.png

Lambdaデプロイ

デプロイの方法も以下の記事を参考にしてください。

Lambdaのテスト

作成されたLambdaをコンソールから開き、[テスト]タブの「テスト」ボタンをクリックすることで動かすことができます。

image.png

成功すると、ログ出力の場所にGCPの指定したプロジェクトのバケット名が表示されます。

image.png

おわりに

今回はWorkload Identity連携を使ったセキュアなGCP連携を、AWS SAMで実現してみました。
日本語対応したOCRはAWSにはないためGCPのを用いようと思っていたので、今回のを発展させLambdaでGCPのVison APIを呼び出す関数を作っておく方法に使おうと考えています。

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