I'm working on a project with Python(3.6) & Django(1.10) in which I'm using aws apis but I'm new to aws and don't know how to authenticate a user.
My scenario is: I need to access user's aws resources like projects list, buckets list etc, for that, I need to authenticate the user when making a request to a particular API.
How can I do that in python? I'm new to aws.So, please don't mind my question.
Update:
Here's what i have tried: From views.py:
def boto3_with_role(role_arn, session_prefix, external_id, **kwargs):
"""
Create a partially applied session to assume a role with an external id.
A unique session_name will be generated by {session_prefix}_{time}
`session` can be passed, otherwise the default sesion will be used
see: http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-api.html
"""
sts = boto3.client('sts')
res = sts.assume_role(
RoleArn=role_arn,
RoleSessionName='{}_{}'.format(session_prefix, int(time.time())),
ExternalId=external_id,
)
creds = res['Credentials']
return partial(boto3.session.Session,
aws_access_key_id=creds['AccessKeyId'],
aws_secret_access_key=creds['SecretAccessKey'],
aws_session_token=creds['SessionToken']
)
class AwsAuthentication(LoginRequiredMixin, CreateView):
def post(self, request, *args, **kwargs):
AwsSession = boto3_with_role('ARN_LINK_FROM_CUSTOMER', 'MyPrefix',
'EXTERNAL_ID_FROM_CUSTOMER')
my_session = AwsSession()
client = my_session.resource('s3')
for bucket in client.buckets.all():
print(bucket.name)
return HttpResponse('Your Bucket is: {}'.format(bucket.name))
Now, it returns: botocore.exceptions.ClientError: An error occurred (InvalidClientTokenId) when calling the AssumeRole operation: The security token included in the request is invalid.
Help me, please!
Thanks in advance!