是的,AWS Glue可以使用IAM身份验证连接到Elasticsearch,这需要使用Elasticsearch Service角色和AWS Glue Dev Endpoint。以下是所需的步骤及代码示例:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "es:*" ], "Resource": [ "arn:aws:es:region:account-id:domain/test-domain" ] } ] }
import boto3
glue = boto3.client('glue')
response = glue.create_dev_endpoint( EndpointName='test-endpoint', RoleArn='arn:aws:iam::account-id:role/service-role/aws-glue-service-role', SecurityGroupIds=['sg-12345678'], SubnetId='subnet-12345678', PublicKey='ssh-rsa AAA... user@host' )
import boto3 from elasticsearch import Elasticsearch, RequestsHttpConnection from requests_aws4auth import AWS4Auth
region = 'region' session = boto3.Session(region_name=region) credentials = session.get_credentials() awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, 'es', session_token=credentials.token)
host = 'search-test-domain-abc123xyz.region.es.amazonaws.com' es = Elasticsearch( hosts=[{'host': host, 'port': 443}], http_auth=awsauth, use_ssl=True, verify_certs=True, connection_class=RequestsHttpConnection )
es.search(index="test-index", body={"query": {"match_all": {}}}) es.index(index="test-index", id=1, body={"text": "example"})