Can a SSE:KMS Key ID be specified when writing to S3 in an AWS Glue Job?

瘦欲@ 提交于 2019-12-24 10:38:13

问题


If you follow the AWS Glue Add Job Wizard to create a script to write parquet files to S3 you end up with generated code something like this.

datasink4 = glueContext.write_dynamic_frame.from_options(
    frame=dropnullfields3,
    connection_type="s3",
    connection_options={"path": "s3://my-s3-bucket/datafile.parquet"},
    format="parquet",
    transformation_ctx="datasink4",
)

Is it possible to specify a KMS key so that the data is encrypted in the bucket?


回答1:


glue scala job

val spark: SparkContext = new SparkContext()
val glueContext: GlueContext = new GlueContext(spark)
spark.hadoopConfiguration.set("fs.s3.enableServerSideEncryption", "true")
spark.hadoopConfiguration.set("fs.s3.serverSideEncryption.kms.keyId", args("ENCRYPTION_KEY"))

I think syntax should be differ for Python, but idea the same




回答2:


To spell out the answer using PySpark, you can do either

from pyspark.conf import SparkConf
[...]
spark_conf = SparkConf().setAll([
  ("spark.hadoop.fs.s3.enableServerSideEncryption", "true"),
  ("spark.hadoop.fs.s3.serverSideEncryption.kms.keyId", "<Your Key ID>")
])
sc = SparkContext(conf=spark_conf)

noticing the spark.hadoop prefix - or (uglier but shorter)

sc._jsc.hadoopConfiguration().set("fs.s3.enableServerSideEncryption", "true")
sc._jsc.hadoopConfiguration().set("fs.s3.serverSideEncryption.kms.keyId", "<Your Key ID>")

where sc is your current SparkContext.




回答3:


This isn't necessary. Perhaps it was when the question was first posed, but the same can be achieved by creating a security configuration and associating that with the glue job. Just remember to have this in your script, otherwise it won't do it:

job = Job(glueContext) 
job.init(args['JOB_NAME'], args)

https://docs.aws.amazon.com/glue/latest/dg/encryption-security-configuration.html https://docs.aws.amazon.com/glue/latest/dg/set-up-encryption.html



来源:https://stackoverflow.com/questions/49405198/can-a-ssekms-key-id-be-specified-when-writing-to-s3-in-an-aws-glue-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!